在用户在第一个 WebView 上获得授权后,我需要在第二个 WebView 上使用相同的配置(cookie)。在模拟器上运行良好,但在设备上不稳定。
class auth: UIViewController, WKNavigationDelegate, UIWebViewDelegate {
private var webView: WKWebView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
loadPage(urlString: link)
}
private func loadPage(urlString: String) {
if let encodedURL = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let url = URL(string: encodedURL) {
let request = URLRequest(url: url)
webView.refreshCookies()
webView.load(request)
}
}
}
extension WKWebView {
func refreshCookies() {
self.configuration.websiteDataStore = WKWebsiteDataStore.default()
}
}