iOS-未设置网络视图Cookie

时间:2018-10-22 12:27:35

标签: ios swift cookies uiwebview wkwebview

我试图像这样在我的iOS中设置cookie:

    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)

然后我像这样打印它们:

    viewerWebKit.configuration.websiteDataStore.httpCookieStore.getAllCookies( { (cookies) in
        cookies.forEach({ (cookie) in
            print(cookie.name)
        })
    })

所有cookie均已打印,并且似乎设置正常。但是,当我使用野生动物园的Web检查器查看时,如果确实设置了它们,则什么也没有。未设置Cookie。问题是什么?我需要接受他们吗?野生动物园是否阻止了他们?如何正常设置它们以使其在Web检查器中可见?

我也尝试过这种方法:

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

}

但是它也不起作用。

这是我在Safari调试控制台中看到的内容: debug console

未设置Cookie。

这就是我在Xcode控制台中看到的内容。

xcode console

因此,这里似乎已设置。但这不是现实。打印代码将打印cookie。但是它们并不是在Safari控制台中都可见。那怎么可能? Cookies csrftoken和sessionid由网站设置,而不是由我的应用程序设置。它们在打印和调试控制台中均可见。

1 个答案:

答案 0 :(得分:0)

通过此设置cookie。也可以在请求创建集之后

urlRequest.httpShouldHandleCookies = true


self.configuration.websiteDataStore.httpCookieStore.setCookie("your_http_cookie", completionHandler: {

//做任何您想做的事。我建议在cookie集加载后加载您的webview。

 })

在您的课程中实施此观察者 WKHTTPCookieStoreObserver

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