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

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

问题描述

我在我的原生iPhone应用程序中,在允许登录/注册的网站上使用WKWebView,并将会话信息存储在cookie中。我试图弄清楚如何持久存储cookie信息,所以当应用程序重新启动时,用户仍然可以使用他们的Web会话。

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?

推荐答案

这实际上是一个因为有一个)一些错误仍然没有被Apple解决(我认为)和b)取决于关于你想要什么样的饼干,我想。

This is actually a though 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。这个似乎有些错误,显然没有立即为 NSHTTPCookieStorage 保存cookie来查找它们。 人员建议通过重置进程池来触发保存,但我不知道这是否可靠。但是,您可能希望自己尝试一下。

  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持久存储Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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