WKWebView 持久存储 Cookies [英] WKWebView Persistent Storage of Cookies

查看:57
本文介绍了WKWebView 持久存储 Cookies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的本机 iPhone 应用程序中使用 WKWebView,在一个允许登录/注册的网站上,并将会话信息存储在 cookie 中.我想弄清楚如何持久存储 cookie 信息,以便当应用程序重新启动时,用户仍然可以使用他们的网络会话.

I am using a WKWebView in my native iPhone application, on a website that allows login/registration, and stores the session information in cookies. I am trying to figure out how to persistently store the cookie information, so when the app restarts, the user still has their web session available.

我在应用中有 2 个 WKWebViews,它们共享一个 WKProcessPool.我从共享进程池开始:

I have 2 WKWebViews in the app, and they share a WKProcessPool. I start with a shared process pool:

WKProcessPool *processPool = [[WKProcessPool alloc] init];

然后对于每个 WKWebView:

Then for each WKWebView:

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; 
theConfiguration.processPool = processPool; 
self.webView = [[WKWebView alloc] initWithFrame:frame configuration:theConfiguration];

当我使用第一个 WKWebView 登录,然后一段时间后将操作传递给第二个 WKWebView 时,会话被保留,因此 cookie 成功共享.但是,当我重新启动应用程序时,会创建一个新的进程池并销毁会话信息.有没有办法让会话信息在应用重启后保持不变?

When I log in using the first WKWebView, and then some time later pass the action to the 2nd WKWebView, the session is retained, so the cookies were successfully shared. However, when I relaunch the app, a new process pool is created and the session information is destroyed. Is there any way to get the session information to persist through an app restart?

推荐答案

这实际上很难,因为有 a) 一些 bug 仍然没有被 Apple 解决(我认为)和 b)取决于你想要什么 cookie,我认为.

This is actually a tough one because there's a) some bug that's still not solved by Apple (I think) and b) depends on what cookies you want, I think.

我现在无法对此进行测试,但我可以给您一些提示:

I wasn't able to test this now, but I can give you some pointers:

  1. NSHTTPCookieStorage.sharedHTTPCookieStorage() 获取 cookie.这个看起来有问题,显然 cookie 没有立即保存以供 NSHTTPCookieStorage 找到它们.人们建议通过重置进程池来触发保存,但我不知道这是否可靠.不过,您可能想亲自尝试一下.
  2. 进程池并不是真正保存 cookie 的东西(尽管它定义了它们是否如您正确所述的那样共享).文档说那是 WKWebsiteDataStore,所以我会查一下.至少可以使用 fetchDataRecordsOfTypes:completionHandler: 从那里获取 cookie(但不确定如何设置它们,而且我假设您不能出于同样的原因将商店保存在用户默认值中至于进程池).
  3. 如果您设法获得所需的 cookie(或者更确切地说是它们的值),但无法恢复它们,我猜是这样,请查看 这里(基本上它展示了如何简单地准备 httprequest 与他们已经,相关部分:[request addValue:@"TeskCookieKey1=TeskCookieValue1;TeskCookieKey2=TeskCookieValue2;" forHTTPHeaderField:@"Cookie"]).
  4. 如果所有其他方法都失败了,请检查这个.我知道仅提供仅链接的答案并不好,但我无法复制所有内容,只是为了完整起见想添加它.
  1. Getting cookies from NSHTTPCookieStorage.sharedHTTPCookieStorage(). This one seems buggy, apparently the cookies aren't immediately saved for NSHTTPCookieStorage to find them. People suggest to trigger a save by resetting the process pool, but I don't know whether that reliably works. You might want to try that out for yourself, though.
  2. The process pool is not really what saves the cookies (though it defines whether they are shared as you correctly stated). The documentation says that's WKWebsiteDataStore, so I'd look that up. At least getting the cookies from there using fetchDataRecordsOfTypes:completionHandler: might be possible (not sure how to set them, though, and I assume you can't just save the store in user defaults for the same reason as for the process pool).
  3. Should you manage to get the cookies you need (or rather their values), but not be able to restore them as I guess will be the case, look here (basically it shows how to simply prepare the httprequest with them already, relevant part: [request addValue:@"TeskCookieKey1=TeskCookieValue1;TeskCookieKey2=TeskCookieValue2;" forHTTPHeaderField:@"Cookie"]).
  4. If all else fails, check this. I know providing just link only answers is not good, but I can't copy all that and just want to add it for completeness sake.

最后一件事:我说过,您的成功可能还取决于 cookie 的类型.这是因为 这个答案 指出服务器设置的 cookie 无法通过 NSHTTPCookieStorage 访问.我不知道这是否与您有关(但我猜是这样,因为您可能正在寻找会话,即服务器设置的 cookie,对吗?)而且我不知道这是否意味着其他方法失败

One last thing in general: I said that your success might also depend on the type of cookie. That's because this answer states that cookies set by the server are not accessible via NSHTTPCookieStorage. I don't know whether that's relevant to you (but I guess it is, since you're probably looking for a session, i.e. server-set cookie, correct?) and I don't know whether this means that the other methods fail as well.

如果所有其他方法都失败了,您可以考虑将用户凭据保存在某处(例如钥匙串),并在下一个应用程序开始自动进行身份验证时重用它们.这可能不会恢复所有会话数据,但考虑到用户退出可能实际上是可取的应用程序?此外,也许可以使用注入的脚本捕获并保存某些值以供以后使用,例如在此处中提到的(显然不是用于设置它们)在开始时,但可能会在某个时候检索它们.当然,您需要知道该站点是如何工作的).

If all else fails, you might consider saving the users credentials somewhere (keychain, for example) and reuse them on the next app start to auth automatically. This might not restore all session data, but considering the user quit the app that's maybe actually desirable? Also perhaps certain values can be caught and saved for later use using an injected script, like mentioned here (obviously not for setting them at start, but maybe retrieve them at some point. You need to know how the site works then, of course).

我希望这至少可以为您指明一些解决问题的新方向.看起来它并不像它应该的那样微不足道(再说一次,会话 cookie 是一种与安全相关的东西,所以也许将它们隐藏在应用程序之外是 Apple 有意识的设计选择......).

I hope that could at least point you towards some new directions solving the issue. It's not as trivial as it should be, it seems (then again, session cookies are kind of a security relevant thing, so maybe hiding them away from the App is a conscious design choice by Apple...).

这篇关于WKWebView 持久存储 Cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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