WKWebView 是否使用来自 Safari 的 cookie? [英] Does WKWebView uses cookies from Safari?

查看:41
本文介绍了WKWebView 是否使用来自 Safari 的 cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 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:

  1. 使用 SFSafariViewController 加载 Safari 视图控制器(或为 iOS8 及以下版本启动 Safari [[UIApplication sharedApplication] openURL:url])
  2. 您在 url 处的脚本会尝试加载您的自定义 URL 方案并传递 cookie 数据,即 my-custom-scheme:cookie=value
  3. 然后您的应用程序将在 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 中接收 cookie 数据
  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

然而,用户会看到 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆