Can I set the cookies to be used by a WKWebView?
Edit for iOS 11+ only
Use WKHTTPCookieStore:
let cookie = HTTPCookie(properties: [
.domain: "example.com",
.path: "/",
.name: "MyCookieName",
.value: "MyCookieValue",
.secure: "TRUE",
.expires: NSDate(timeIntervalSinceNow: 31556926)
])!
webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie)
Since you are pulling them over from HTTPCookeStorage, you can do this:
let cookies = HTTPCookieStorage.shared.cookies ?? []
for cookie in cookies {
webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie)
}
Old answer for iOS 10 and below
If you require your cookies to be set on the initial load request, you can set them on NSMutableURLRequest. Because cookies are just a specially formatted request header this can be achieved like so:
WKWebView * webView = /*set up your webView*/
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/index.html"]];
[request addValue:@"TeskCookieKey1=TeskCookieValue1;TeskCookieKey2=TeskCookieValue2;" forHTTPHeaderField:@"Cookie"];
// use stringWithFormat: in the above line to inject your values programmatically
[webView loadRequest:request];
If you require subsequent AJAX requests on the page to have their cookies set, this can be achieved by simply using WKUserScript to set the values programmatically via javascript at document start like so:
WKUserContentController* userContentController = WKUserContentController.new;
WKUserScript * cookieScript = [[WKUserScript alloc]
initWithSource: @"document.cookie = 'TeskCookieKey1=TeskCookieValue1';document.cookie = 'TeskCookieKey2=TeskCookieValue2';"
injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
// again, use stringWithFormat: in the above line to inject your values programmatically
[userContentController addUserScript:cookieScript];
WKWebViewConfiguration* webViewConfig = WKWebViewConfiguration.new;
webViewConfig.userContentController = userContentController;
WKWebView * webView = [[WKWebView alloc] initWithFrame:CGRectMake(/*set your values*/) configuration:webViewConfig];
Combining these two techniques should give you enough tools to transfer cookie values from Native App Land to Web View Land. You can find more info on the cookie javascript API on Mozilla's page if you require some more advanced cookies.
Yeah, it sucks that Apple is not supporting many of the niceties of UIWebView. Not sure if they will ever support them, but hopefully they will get on this soon.
Getting all cookies from WKWebView
Finally, httpCookieStore
for WKWebsiteDataStore
landed in iOS 11.
https://developer.apple.com/documentation/webkit/wkwebsitedatastore?changes=latest_minor
Related Topics
What Is the Expected Behaviour of Facebook Login
How to Play Multiple Audio Files Simultaneously
Uilongpressgesturerecognizer on Uitableviewcell - Double Call
Dequeuereusablecellwithidentifier:Forindexpath: VS Dequeuereusablecellwithidentifier:
Pfobject' Does Not Have a Member Named 'Subscript'
iOS 10.3 Beta 3 Doesn't Persist Data of Keychainitem
Displaying Youtube Video Ads with Youtube Iframe
iOS 7.0 and Arc: Uitableview Never Deallocated After Rows Animation
Code Signing Issue in Xcode Version 8
iOS Lazy-Loading of Table Images
iOS 11 - Unable to Change Navigation Bar Height
How to Take Uiimage of Avcapturevideopreviewlayer Instead of Avcapturephotooutput Capture
Custom Uisearchbar with Uisearchcontroller
Making Uitableview with Embedded Uicollectionview Using Uitableviewautomaticdimension
Xcode 4.2 Mainstoryboard Not Found
Expired Access Token After Openactivesession for Facebook iOS Sdk
I Just Want to Open Ms Outlook App and See Mailto Screen Using Url Scheme at iOS