Does WKWebView uses cookies from Safari?

ios, nshttpcookie, wkwebview

Solution

iOS security sandbox disables cookie sharing amongst apps using `WKWebView` as you've discovered with the iOS note and there's no way around this. You can share cookies between multiple `WKWebView`'s inside your app by utilising `WKProcessPool`.

There is a way of passing cookie data from Safari to your app by combining `SFSafariViewController` (iOS 8 and below you will need to switch to Safari) with a custom URL scheme.

The fundamentals of this approach would be:

- Load a Safari View Controller using `SFSafariViewController` (or launch Safari `[[UIApplication sharedApplication] openURL:url]` for iOS8 and below)

- Your script at `url` would attempt to load your custom URL scheme passing cookie data i.e. `my-custom-scheme:cookie=value`

- Your app will then receive the cookie data in `- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation`

The user will however see the Safari View Controller pop up then close (or switch between apps for iOS8 and below). There isn't a silent way of doing this (extra: There are ways to force the main window to be above the `SFSafariViewController` however this isn't supported by Apple).

Problem

According to Apple documentation: ... cookies are shared among all applications and are kept in sync across process boundaries. But there is this iOS Note: Cookies are not shared among applications in iOS. So basically I can not use cookies from Safari inside my app?

Original source