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];
