How to enable and get cookies from WKWebView?

落爺英雄遲暮 提交于 2020-01-06 04:37:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!