Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having separate cookie storage for two 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.

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!

like image 685
howanghk Avatar asked May 15 '13 07:05

howanghk


1 Answers

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];
like image 157
bdev Avatar answered Oct 21 '22 12:10

bdev