3
votes

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?

1

1 Answers

9
votes

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:

  1. Load a Safari View Controller using SFSafariViewController (or launch Safari [[UIApplication sharedApplication] openURL:url] for iOS8 and below)
  2. Your script at url would attempt to load your custom URL scheme passing cookie data i.e. my-custom-scheme:cookie=value
  3. 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).