Lydia Dai
Lydia Dai

Reputation: 69

ios12-Get set-cookie from NSHTTPURLResponse

I got set-cookie in decidePolicyForNavigationResponse method before like this:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
       NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
       self.response = response;
}

But in ios12 navigationResponse in this method cannot get set-cookie again.Can I have other method to replace it? Or is there any other method that I can get set-cookie on page? If you cannot understand please let me know. Thank you~

Upvotes: 2

Views: 1310

Answers (1)

WeiGo
WeiGo

Reputation: 56

I encountered the same issue as yours. I guess there is no longer retrieving cookies via WKNavigationResponse. (started from iOS 12.*)

the cookies of WKWebview are stored in the NSHTTPCookieStorage.sharedHTTPCookieStorage()

you could try to redesign the code as below instead of yours

Objective-C

if (@available(iOS 11.0, *)) {  //available on iOS 11+
    WKHTTPCookieStore *cookieStore = webView.configuration.websiteDataStore.httpCookieStore;
        [cookieStore getAllCookies:^(NSArray* cookies) {
            if (cookies.count > 0) {
                for (NSHTTPCookie *cookie in cookies) {
                    //TODO...
                }
            }
        }];
}

Swift 4

if #available(iOS 11, *) {
    webView.configuration.websiteDataStore.httpCookieStore.getAllCookies({ (cookies) in
        for cookie in cookies {
              //TODO...   
        }
    })
}

Above codes are available on iOS 11+, if your app is supported for lower version your should separate above code with yours.

hope it's works for you.

Upvotes: 4

Related Questions