我想用不同的webView登录同一站点的许多帐户。例如,我的Tab Bar Controller包含三个 View Controller ,每个 View Controller 都包含webView。例如,我将stackoverflowwebView网址嵌入每个类中。用户如何使用这三个webView同时登录不同的帐户?我已经尝试过了,但是一次只能登录一个用户。
我发现我需要为每个cookie创建单独的UIWebView,但是大多数答案都在Objective-C中,而不是我想要的正确答案。例如(First Second Third)
谁能告诉我我该怎么做?

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()
    }


}

谢谢

ios - 两个(UIWebView或WKWebView)的单独cookie存储-LMLPHP

最佳答案

您可以通过使用WKWebView的不同实例来使用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)

不幸的是,在应用重启后,您将丢失webView数据(例如cookie,缓存等),因为无法将非持久WKWebsiteDataStore保存到磁盘上(您可能会注意到WKWebsiteDataStore实现了NSCoding,但不适用于非ojit_code持久存储)。

09-05 07:42