问题
I want to enable the cookies of WKWebView so that if user log in gmail or solve recaptcha in WKWebView, it should not be asked to log in gmail or solve recaptcha again.
I also want to get those cookies so that I can pass those cookies to other instance of WKWebView on other screen.
I am using following code to get cookies but it is returning 0 count of cookies.
[dataStore.httpCookieStore getAllCookies:^(NSArray *cookies) {
[self->settings setCookies:cookies];
[[NSNotificationCenter defaultCenter] postNotificationName:@"browserShouldReload" object:nil];
[self.navigationController popViewControllerAnimated:YES];
}];
please let me how to achieve this.
回答1:
To manager the cookies of a application context uses
HTTPCookieStorage shared instance
To get all the cookies:
HTTPCookieStorage.shared.cookies
it will bring all the cookies in a HTTPCookie array if exists[HTTPCookie]?
also you can get the cookies for a specific url
HTTPCookieStorage.shared.cookies(for: "https://www.gmail.com")
if you want you can remove all cookies too, from a date that you want
HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
And a lot of others things with HTTPCookieStorage.shared object
see HTTPCookieStorage on the Apple Developer Documentation for more
来源:https://stackoverflow.com/questions/55990796/how-to-enable-and-get-cookies-from-wkwebview