iOS-未设置网络视图Cookie [英] iOS - web view cookies not set
问题描述
我试图像这样在我的iOS中设置cookie:
I am trying to set cookies in my iOS like this:
let url = URL(string: "url")!
let jar = HTTPCookieStorage.shared
let cookieHeaderField = ["Set-Cookie": "key1=value1, key2=value2"]
let cookies = HTTPCookie.cookies(withResponseHeaderFields: cookieHeaderField, for: url)
jar.setCookies(cookies, for: url, mainDocumentURL: url)
let request = URLRequest(url: url)
viewerWebKit.load(request)
然后我要像这样打印它们:
Then I am printing them like this:
viewerWebKit.configuration.websiteDataStore.httpCookieStore.getAllCookies( { (cookies) in
cookies.forEach({ (cookie) in
print(cookie.name)
})
})
所有cookie均已打印,并且看起来像正常设置。但是,当我使用野生动物园的Web检查器查看时,如果确实设置了它们,则什么也没有。未设置Cookie。问题是什么?我需要接受他们吗?野生动物园是否阻止了他们?如何正常设置它们以使其在Web检查器中可见?
All cookies are printed and they seem to be set normally. But when I use the Web inspector of my safari to see, if they are really set then nothing is there. No cookies are set. What is the problem? Do I need to accept them? Is safari blocking them? How can I set them normally, to be visible in web inspector?
我也尝试过这种方法:
import UIKit
import WebKit
class ViewWrapper: UIViewController, WKNavigationDelegate {
var loginToken: String?
@IBOutlet weak var viewerWebKit: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
var urlRequest = URLRequest(url: URL(string: "url")!)
urlRequest.httpShouldHandleCookies = true
let newcookie = HTTPCookie(properties: [
.domain: "domain",
.path: "",
.name: "key",
.value: "value",
.secure: "FALSE",
.expires: NSDate(timeIntervalSinceNow: 31556926)
])
viewerWebKit.configuration.websiteDataStore.httpCookieStore.setCookie(newcookie!, completionHandler: {
self.viewerWebKit.load(urlRequest)
})
viewerWebKit.configuration.websiteDataStore.httpCookieStore.getAllCookies( { (cookies) in
cookies.forEach({ (cookie) in
print(cookie.name)
})
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func loadView() {
viewerWebKit = WKWebView()
viewerWebKit.navigationDelegate = self
view = viewerWebKit
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
func cookiesDidChange(in cookieStore: WKHTTPCookieStore) {
cookieStore.getAllCookies({ (cookies) in
cookies.forEach({ (cookie) in
print(cookie.name)
})
})
}
}
但没有用太。
这是我在野生动物园调试控制台中看到的:
This is what I see in safari debug console:
未设置Cookie。
这是我在Xcode控制台中看到的。
This is what I see in Xcode's console.
因此似乎已在此处设置。但这不是现实。打印代码将打印cookie。但是它们并不是在Safari浏览器控制台中都可见。那怎么可能? Cookies csrftoken和sessionid由网站设置,而不是由我的应用程序设置。它们在打印和调试控制台中均可见。
So here it seems to be set. But it is not in reality. Printing code prints cookies. But they are not all visible in safari console. How is that possible? Cookies csrftoken and sessionid are set by website, not by my app. And they are visible in both printing and debug console.
推荐答案
通过此设置cookie。也在请求创建后
Set cookie through this. also after request create set
urlRequest.httpShouldHandleCookies = true
self.configuration.websiteDataStore.httpCookieStore.setCookie("your_http_cookie", completionHandler: {
//随心所欲。建议在Cookie设置后加载您的网络视图。
//do whatever you want. i suggest after cookie set load your webview.
})
在您的班级中添加此观察者 WKHTTPCookieStoreObserver 。
Implement this observers WKHTTPCookieStoreObserver in your class.
WKWebsiteDataStore.default().httpCookieStore.add(self)
func cookiesDidChange(in cookieStore: WKHTTPCookieStore) {
// Must implement otherwise wkwebview cookie not sync properly
self.httpCookieStore.getAllCookies { (cookies) in
cookies.forEach({ (cookie) in
// print your cookie here
})
}
}
这篇关于iOS-未设置网络视图Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!