9

I want to login many accounts of same site in different webView. For example i have Tab Bar Controller that contains three view controllers and each view controllers contain webView. And for example i embed stackoverflow url for webView in every class. How user can login to different accounts at the same time using these three webView? I've tried this but i can just login one user at a time. I have found that i need to create separate cookie for every UIWebView, but mostly answers are in objective-c and not the proper answer i want. For example (First Second Third) Can any one please tell me how i can do it?

class FirstViewController: UIViewController , UIWebViewDelegate{

    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    override func viewDidLoad() {
        webView.delegate = self
        let requestURL = NSURL(string: "http://stackoverflow.com")
        let request = NSURLRequest(URL: requestURL!)
        activityIndicator.hidesWhenStopped = true
        activityIndicator.startAnimating()
        webView.loadRequest(request)

    }
       func webViewDidFinishLoad(webView: UIWebView) {
        activityIndicator.stopAnimating()
    }

}

class SecondViewController: UIViewController, UIWebViewDelegate{

    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    override func viewDidLoad() {
        webView.delegate = self
        let requestURL = NSURL(string: "http://stackoverflow.com")
        let request = NSURLRequest(URL: requestURL!)
        activityIndicator.hidesWhenStopped = true
        activityIndicator.startAnimating()
        webView.loadRequest(request)

    }
        func webViewDidFinishLoad(webView: UIWebView) {
        activityIndicator.stopAnimating()
    }


}

Thanks

The preview of my executing code.

Community
  • 1
  • 1
ZAFAR007
  • 2,545
  • 1
  • 26
  • 40
  • I read many posts about separate cookie jar or storage per (uiwebview, wkwebview) but did't get any help. I check that is also very difficult in OS X (http://stackoverflow.com/questions/364219/how-can-i-have-multiple-instances-of-webkit-without-sharing-cookies) (http://stackoverflow.com/questions/28456789/separate-cookie-jar-per-webview-in-os-x) (https://github.com/jjconti/swift-webview-isolated) (https://github.com/cyyuen/ADCookieIsolatedWebView) Any one have any idea how i can achieve this?? – ZAFAR007 Aug 22 '16 at 16:43
  • did you try [this](http://igisolatedcookiewebview.googlecode.com/)? – ddb Aug 24 '16 at 07:31
  • @ddb yes i was check that but this is for OS X. Cocoa is the Mac development framework. It doesn't exist on iOS. – ZAFAR007 Aug 24 '16 at 08:00
  • @ddb right now, your link not on a server show so can you send me detail of OSX for Separate cookie storage for two (UIWebView or WKWebView). – ronak patel Dec 01 '17 at 11:52
  • sorry @ronakpatel but that code wasn't mine, in fact I didn't made an answer, only posted a comment – ddb Dec 01 '17 at 13:08
  • I have the same issue, it's solved? – Mickael Belhassen Nov 30 '19 at 16:44

1 Answers1

10

You can do this with WKWebView by using different instances of WKWebSiteDataStore:

let configuration1 = WKWebViewConfiguration()
configuration1.websiteDataStore = WKWebsiteDataStore.nonPersistent()
self.webView1 = WKWebView(frame: CGRect.zero, configuration: configuration1)
self.view.addSubview(self.webView1)

let configuration2 = WKWebViewConfiguration()
configuration2.websiteDataStore = WKWebsiteDataStore.nonPersistent()
self.webView2 = WKWebView(frame: CGRect.zero, configuration: configuration2)

Unfortunately, you will loose webView data (such as cookies, cache, etc) after app restart, because non-persistent WKWebsiteDataStore can't be saved to disk (you could notice that WKWebsiteDataStore implements NSCoding, but it doesn't work for non-persistent stores).

Roman Ermolov
  • 7,750
  • 5
  • 24
  • 34
  • Ermolov, Thanks. I was seen [WKWebSiteDataStore](https://developer.apple.com/library/ios/documentation/WebKit/Reference/WKWebsiteDataStore_Class_Ref/) , but this is only avialable in iOS 9. Have you any idea how i can do this in iOS 8. Also i check your code it gives me that error **Type 'WKWebsiteDataStore' has no member 'nonPersistent'** ?? – ZAFAR007 Aug 24 '16 at 14:31
  • There are private class `_WKWebsiteDataStore` with method `nonPersistentDataStore` and private property `_WKWebsiteDataStore *_websiteDataStore` in `WKWebViewConfiguration` that are available in iOS 8. You can create instance of this class using runtime functions (learn about runtime in Objective-C). Also, you need to know that Apple prohibits to use private classes/methods in production code, but you can try it in your test project. The error above related to version of Swift you are using. I use Swift 3 (Xcode 8 beta), you might need to change the name of method to `nonPersistentDataStore`. – Roman Ermolov Aug 24 '16 at 14:44
  • Thanks now it works. you mean i can only use private class/methods for test project not for deployment on app store. I have one more question to you, can you please tell is there any way to save cookie on storage using this method for next time opening wkwebview from where i leave it after restart app?? – ZAFAR007 Aug 24 '16 at 15:18
  • Please read my answer above; as far as I know, it is impossible. – Roman Ermolov Aug 24 '16 at 16:32
  • @ Roman Ermolov hi your code in some issue for can u plz solve for me when I back tab2 to tab 1 at that time clear cookies types of function work and show page login page not show already login page.so plz help me I m new ios developer so cant understand in this situation what should I do. – ronak patel Dec 07 '17 at 09:00
  • It's worth mentioning that you can't use the aspects of `NSCoding` to persist data for _any_ type of `WKWebsiteDataStore`. For example, if you get a copy of `WKWebsiteDataStore.default()`, persist it, then load it at the next app launch, it will load and function, but it too will not have any data associated with it. As near as I can tell, the `NSCoding` implementation is not generally functional for consumers, in the way they would expect. – KevinH Jan 12 '18 at 02:43
  • This worked for me when I had a caching issue between NSURLSession and wkWebview. Thank you! – TimBigDev Jun 05 '20 at 05:27