我希望在重定向到特定网址后从WKWebView
获取所有Cookie。
我无法获取所有Cookie,但有些Cookie丢失。
private func addObserver() {
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(didChangeCookiesWithNotification(_:)),
name: NSHTTPCookieManagerCookiesChangedNotification,
object: nil)
}
@objc private func didChangeCookiesWithNotification(notification: NSNotification?) {
print("cookies changed")
print("Finish cookies location = \(NSHTTPCookieStorage.sharedHTTPCookieStorage())")
print("Finish cookies count = \(NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies?.count)")
print("Finish cookies = \(NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies?.map { "\($0.name) --- Domain = \($0.properties?["Domain"])" } )")
}
func webView(webView: WKWebView,
didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
print("redirected URL = \(webView.URL)")
print("Redirect cookies location = \(NSHTTPCookieStorage.sharedHTTPCookieStorage())")
print("Redirect cookies count = \(NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies?.count)")
print("Redirect cookies = \(NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies?.map { "\($0.name) --- Domain = \($0.properties?["Domain"])" } )")
}
//Inside didReceiveServerRedirectForProvisionalNavigation
webView.configuration.processPool = WKProcessPool()
答案 0 :(得分:0)
据我所知,只有一种工作方法 - 从JavaScript获取cookie。你无法获得HTTP-cookies,但对其他类型的人来说还可以。
您可以使用相同的流程池创建后台webView
,并使用loadHTMLString:baseURL:
将重定向网址作为基本网址加载空html。
之后,您需要处理通过webView:didReceiveServerRedirectForProvisionalNavigation:
重定向的事实。当收到有关重定向的通知时,在后台webView上调用JS - 您将收到相同的cookie。