12

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!

user7388
  • 1,741
  • 2
  • 19
  • 25
howanghk
  • 3,070
  • 2
  • 21
  • 34

2 Answers2

1

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];
bdev
  • 2,060
  • 5
  • 24
  • 32
  • in `webViewDidStartLoad`, the request is already sent, therefore your hack didn't work. Moving it to `shouldStartLoadWithRequest` might work. However, your hack won't allow both UIWebView to load request simultaneously. – howanghk Jul 17 '13 at 11:30
  • @bdev answer I want in swift 3.0 can help me. – ronak patel Dec 01 '17 at 11:26
0

If you're willing to dig a little deeper, the used-to-be famous ASIHttpRequest knew how to arrange this back in the day. Read switching cookie storage for requests. They also have an interesting wrapper for UIWebView requests, AKA ASIWebPageRequest.

Sadly, the project has since been discontinued, but it should provide you with an operation ground to achieve your goal.

Otherwise, as bdev has presented, I would queue the requests and replace the cookie storage each time, before firing a request. You could use the dispatcher nicely here.

Ben Max Rubinstein
  • 1,803
  • 1
  • 13
  • 15