WKWebView 是否使用来自 Safari 的 cookie? [英] Does WKWebView uses cookies from Safari?
问题描述
根据 Apple 文档:
According to Apple documentation:
... cookie 在所有应用程序之间共享,并跨进程边界保持同步.
... cookies are shared among all applications and are kept in sync across process boundaries.
但是有这个iOS Note:
Cookie 不会在 iOS 中的应用程序之间共享.
Cookies are not shared among applications in iOS.
所以基本上我不能在我的应用程序中使用来自 Safari 的 cookie?
So basically I can not use cookies from Safari inside my app?
推荐答案
iOS 安全沙箱使用 WKWebView
禁用应用程序之间的 cookie 共享,正如您在 iOS 注释中发现的那样,没有办法解决这个问题.您可以使用 WKProcessPool
在应用内的多个 WKWebView
之间共享 cookie.
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
.
通过将 SFSafariViewController
(iOS 8 及更低版本,您需要切换到 Safari)与自定义 URL 方案相结合,有一种方法可以将 cookie 数据从 Safari 传递到您的应用.
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:
- 使用
SFSafariViewController
加载 Safari 视图控制器(或为 iOS8 及以下版本启动 Safari[[UIApplication sharedApplication] openURL:url]
) - 您在
url
处的脚本会尝试加载您的自定义 URL 方案并传递 cookie 数据,即my-custom-scheme:cookie=value
- 然后您的应用程序将在
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
中接收 cookie 数据
- 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
然而,用户会看到 Safari 视图控制器弹出然后关闭(或在 iOS8 及以下的应用程序之间切换).没有一种静默的方式来做到这一点(额外的:有一些方法可以强制主窗口位于 SFSafariViewController
上方,但 Apple 不支持).
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).
这篇关于WKWebView 是否使用来自 Safari 的 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!