20

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.

4 Answers 4

18

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.

8
  • 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
    Dec 13, 2008 at 11:15
  • 14
    I actually implemented a WebView subclass that does this using the method described in this answer. igisolatedcookiewebview.googlecode.com (New BSD license).
    – Isaac
    Mar 12, 2010 at 22:07
  • @Isaac Wow, absolutely perfect for what I need. One problem I noticed is in line 152 of your .m file (initializing cookieStore array), if you are using ARC and remove the retain call, it crashes. I just had to use -fno-objc-arc on that file.
    – sudo
    Sep 29, 2013 at 1:30
  • This solution doesn't handle correctly cookies set by js document.cookie, does it? Feb 24, 2015 at 16:26
  • I found a project trying to solve the problem and it claims that it handles both request-level and JS-level cookies. github.com/cyyuen/ADCookieIsolatedWebView
    – Seunghoon
    Aug 6, 2015 at 9:18
3

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?

0

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.

1
  • 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
    Dec 12, 2008 at 22:39
0

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.

1
  • 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
    Dec 22, 2008 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.