两个 UIWebView 有单独的 cookie 存储吗? [英] Having separate cookie storage for two UIWebView?

查看:21
本文介绍了两个 UIWebView 有单独的 cookie 存储吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景
我正在开发一个简单的 iPad 应用程序,它允许用户同时使用不同的登录名浏览同一个网站.因此,我有两个 UIWebView 并且它们应该有不同的 cookie 存储,因此用户可以在第一个 UIWebView 上登录一个帐户,在第二个 UIWebView 上登录另一个帐户>.

Background
I am developing a simple iPad application that allow the user to browse the same website with different logins at the same time. Therefore I have two UIWebView and they should have different cookie storage so the user can login one account on the first UIWebView and another account on the second UIWebView.

我尝试了什么?
我认为解决方案是在我拥有的两个 UIWebView 中实现不同的 cookie 存储.

What have I tried?
I think the solution is to implement different cookie storages in the two UIWebView I have.

Sasmito Adibowo 写了一篇文章实施您自己的 Cookie 存储,其中详细介绍了如何在 Mac 上为 WebView 使用自定义 cookie 存储.
它是通过修改 WebView 将要发送的 NSURLRequest 来完成的,向其中添加 cookie 标头,并拦截来自 WebView 的响应并从响应标头中提取 cookie 并将其保存到我们自己的 cookie 存储中.
从技术上讲,它是通过实现这两个委托方法来完成的:

Sasmito Adibowo wrote an article Implementing Your Own Cookie Storage which provide details on how to use a custom cookie storage for WebView on Mac.
It is done by modify the NSURLRequest that WebView is going to send, adding cookie headers to it, and also intercept the response from WebView and extract the cookies from the response header and save it to our own cookie storage.
Technically, it is done by implementing these two delegate methods:

- (void)webView:(WebView *)sender resource:(id)identifier didReceiveResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)dataSource
- (NSURLRequest *)webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource

虽然没有记录,但 UIWebView 确实支持上述方法之一,但方法名称略有不同:

Although it is undocumented, UIWebView did support one of the method above with a slightly different method name:

- (NSURLRequest *)uiWebView:(UIWebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(id)dataSource

但是,UIWebView 没有用于 webView:resource:didReceiveResponse:fromDataSource: 的等效委托方法,因此我无法从响应标头中提取 cookie.

However, UIWebView don't have a equivalent delegate method for webView:resource:didReceiveResponse:fromDataSource: and hence I cannot extract the cookies from the response headers.

问题
有没有办法让UIWebView使用自定义的cookie存储,让两个UIWebView可以有自己的cookie存储?

The Question
Is there a way to have UIWebView to use a custom cookie storage, so the two UIWebView can have their own cookie storage?

谢谢!

推荐答案

您是否尝试过在 webViewDidStartLoad 中获取与特定 webview 关联的 cookie(并保留它们):

Have you tried getting the cookies associated with a particular webview (and holding onto them) in webViewDidStartLoad:

NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [cookieJar cookies]) {
    [self.cookies addObject:cookie];
}

并在之后存储这些 cookie(从 self.cookies 中检索值和键):

And storing these cookies right after (retrieve values and keys from self.cookies):

NSMutableDictionary *cookieDict = [NSMutableDictionary dictionary];
[cookieDict setObject:@"value1" forKey:NSHTTPCookieName];
[cookieDict setObject:@"value2" forKey:NSHTTPCookieValue];
[cookieDict setObject:@"value3" forKey:NSHTTPCookieDomain];
...etc..

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieDict];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

您还需要在 viewDidLoad 中看到这一点:

You'll also need to see this in your viewDidLoad:

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

这篇关于两个 UIWebView 有单独的 cookie 存储吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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