WKUserContentController *userContentController = WKUserContentController.new;
NSString *cookieSource = @"document.cookie = 'TeskCookieKey1=TeskCookieValue1';document.cookie = 'TeskCookieKey2=TeskCookieValue2';";
WKUserScript *cookieScript = [[WKUserScript alloc] initWithSource:cookieSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
[userContentController addUserScript:cookieScript];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.userContentController = userContentController;
self.webview = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
以上为WKWebView中创建cookie的方法,亲测有效
网上很多种方法都是基于 WKWebsiteDataStore 的使用,但是和我们的H5配合的时候,怎么都取不到对应的值,只有上面的方法能获取到
WKWebsiteDataStore * webStore = [WKWebsiteDataStore nonPersistentDataStore];
NSHTTPCookie * cookie = [NSHTTPCookie cookieWithProperties:@{
NSHTTPCookieName:@"RefresheEpireTime",
NSHTTPCookieValue:@"2019-05-07",
NSHTTPCookiePath:@"/ui/cookie.html",
NSHTTPCookieDomain:@"test.ripx.com"
}];
[webStore.httpCookieStore setCookie:cookie completionHandler:nil];