Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate cookie storage for two (UIWebView or WKWebView)

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.

like image 695
ZAFAR007 Avatar asked Aug 02 '16 04:08

ZAFAR007


People also ask

Does WKWebView store cookies?

The current app manages the users login / session outside of the webview and sets the cookies required for authentication into the the NSHTTPCookieStore . Unfortunately new WKWebView doesn't use the cookies from the NSHTTPCookieStorage .

Does WKWebView share cookies with Safari?

You can share cookies between multiple WKWebView 's inside your app by utilising WKProcessPool . There is a way of passing cookie data from Safari to your app by combining SFSafariViewController (iOS 8 and below you will need to switch to Safari) with a custom URL scheme.

How do I upgrade UIWebView to WKWebView?

Step-1: Firstly, create a new project with a Single View App. Step-2: Then, open “Main. storyboard” and on your ViewController's view drag “Webkit View”. Select a WKWebView and place it on your view of a view controller.

How do I add WKWebView to my storyboard?

First add the web view as a UIWebView outlet in the storyboard / IB. This will give you a property like this: @property (weak, nonatomic) IBOutlet UIWebView *webView; Then just edit your code to change it to a WKWebView.

How does the WebView handle cookies?

The WebView will handle cookies similar to a regular browser. When making web calls via the native http client, this service also reads and stores cookies as you load webpages that contain them. Thankfully, UWP and iOS share their cookie containers automatically between the WebView and native http client, however Android does not.

What are the downsides of the UWP cookiemanager?

Unfortunately the only downside to the UWP CookieManager, is the inability to delete all cookies in one command, or even get all cookies. You must know the domain of the cookie’s you want to delete and get all cookies from that domain, while deleting one by one.

Is there a way to set the website data store for UIWebView?

I don’t think there’s a way to set this directly (the fact that UIWebView used was more of an historical accident than a specific design goal). However, iOS 9 introduces WKWebsiteDataStore which gives you a bunch of options here, although AFAICT none is the direct equivalent of .

How do I view and edit cookies in a cookie container?

In Windows, if you are using Windows.Web.Http.HttpClient, you can access your cookie container by ensuring you supply the Protocol Filter. By doing this you can access the _filter.CookieManager and from here you can view, add, modify or delete cookies.


1 Answers

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).

like image 137
Roman Ermolov Avatar answered Oct 01 '22 16:10

Roman Ermolov