有两个UIWebView的单独的Cookie存储? [英] Having separate cookie storage for two UIWebView?
问题描述
背景
我开发了一个简单的iPad应用程序,允许用户同时浏览具有不同登录信息的同一网站。因此,我有两个 UIWebView
,他们应该有不同的cookie存储,所以用户可以登录第一个 UIWebView
第二个 UIWebView
上的另一个帐户。
我尝试了什么?
我认为解决方案是在两个 UIWebView
中实现不同的cookie存储库。
Sasmito Adibowo撰写了一篇文章实现自己的Cookie存储,其中提供了有关如何使用自定义Cookie存储的 WebView
在Mac上。
这是通过修改NSURLRequest, WebView
要发送,添加cookie头部,并拦截来自 WebView
的响应,并从响应标头中提取Cookie,并将其保存到我们自己的Cookie存储中。
,它是通过实现这两个委托方法:
- (void)webView:(WebView *)sender resource: )标识符didReceiveResponse:(NSURLResponse *)响应fromDataSource:(WebDataSource *)dataSource
- (NSURLRequest *)webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse: )redirectResponse fromDataSource:(WebDataSource *)dataSource
虽然没有记录,但 UIWebView
确实支持上述方法之一,方法名称稍有不同:
- (NSURLRequest *) uiWebView:(UIWebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(id)dataSource
但是, UIWebView
没有的等效委托方法webView:resource:didReceiveResponse:fromDataSource
$ b $ b是否有一种方法让
UIWebView
使用自定义cookie存储,因此两个 UIWebView
可以有自己的cookie存储? 谢谢!
NSHTTPCookie * cookie;与webviewDidStartLoad中的特定网络视图相关联
NSHTTPCookieStorage * cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for(cookie in [cookieJar cookies]){
[self.cookies addObject:cookie];
}
然后立即存储这些cookie(从self.cookies获取值和键) :
NSMutableDictionary * cookieDict = [NSMutableDictionary dictionary];
[cookieDict setObject:@value1forKey:NSHTTPCookieName];
[cookieDict setObject:@value2forKey:NSHTTPCookieValue];
[cookieDict setObject:@value3forKey:NSHTTPCookieDomain];
... etc ..
NSHTTPCookie * cookie = [NSHTTPCookie cookieWithProperties:cookieDict];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
您还需要在您的viewDidLoad中看到:
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
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
.
What have I tried?
I think the solution is to implement different cookie storages in the two UIWebView
I have.
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
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
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.
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?
Thanks!
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];
}
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];
You'll also need to see this in your viewDidLoad:
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
这篇关于有两个UIWebView的单独的Cookie存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!