我想在重定向到特定 URL 后从 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()
据我所知,只有一种可行的方法——从 JavaScript 获取 cookie。您将无法获取 HTTP-cookies,但对于其他类型就可以了。
您可以使用相同的进程池创建背景 webView
并使用 loadHTMLString:baseURL:
将空 html 加载到其中,并将重定向 URL 作为基本 url。
之后,您需要处理通过 webView:didReceiveServerRedirectForProvisionalNavigation:
进行重定向的事实。收到重定向通知时,在后台 webView 上调用 JS - 您将收到相同的 cookie。
关于ios - WKWebView 获取cookies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38606370/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |