views:

994

answers:

4

I have an app that creates a couple of WebView instances and I'd like to have them operate as independently as possible.

At the very least, I don't want them sharing cookies. A quick google search gave me results liking "you can't." I'm hoping someone has a better answer.

A: 

I would assume that cookies would be configured on a service / application level and not for particular instances or processes. Perhaps you could revise your question to find a way to resolve the problem you are having which requires that the instances do not share cookies.

What is the motivation for not sharing cookies between the instances?

If you just need 3 views into the same web resource you could setup some virtual hosts that point to the same data source.

Will Bickford
WebView is webkit's browser view. I've effectively got two windows looking at the same web page, and I'd like them not to be using the same session.
Dustin
+4  A: 

The basic answer is "you can't".

After looking at this for a bit, I think it's possible, but extremely complicated. It would involve implementing a resourceLoadDelegate on your WebView that implements -webView:resource:willSendRequest:redirectResponse:fromDataSource: and modifies the request to turn off HTTPShouldHandleCookies and adds any relevant cookies to the request manually. It also has to implement -webView:resource:didReceiveResponse:fromDataSource: to find out about any cookies returned from the server. You can alloc/init your own copy of NSHTTPCookieStorage per-webview and use that to store/retrieve the cookies.

Kevin Ballard
Heh. That's amazing. I ended up changing the web service to work around the problems I was having. I would expect this to be kind of common (private mode, multi-user testing, etc...). Thanks for looking into this.
Dustin
I actually implemented a WebView subclass that does this using the method described in this answer. http://igisolatedcookiewebview.googlecode.com/ (New BSD license).
Isaac
+2  A: 

This post sums up what you could do. I'm not sure if it is feasible for you and I feel it wouldn't be a straightforward task, maybe even risky, but it seems to be possible: the author claims iCab does it this way.

I was hoping for a simpler solution too, really. Of course, since Webkit is open source you could just roll out your own version of the framework with changed behavior I guess?

Kristof
A: 

What you can do is take a look at libcurl which can handle cookie stores that don't mix with the URL Loading system wide cookie storage for those requests you want to separate. For me that seems to be a valid and simple solution. If you really need to depend on webview/webkit it might not be.

Kristof
Yeah, the webvivew stuff was pretty important to me. I could probably still make it go, but was hoping to avoid rewriting the world. I ended up changing the server instead. *sigh*
Dustin