Does WKWebView uses cookies from Safari?

坚强是说给别人听的谎言 提交于 2021-01-24 06:59:12

问题


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:


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).



来源:https://stackoverflow.com/questions/40148060/does-wkwebview-uses-cookies-from-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!