How can I have multiple instances of webkit without sharing cookies?
Asked Answered
F

4

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.

Feuilleton answered 12/12, 2008 at 21:34 Comment(0)
J
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.

Jacobine answered 13/12, 2008 at 10:26 Comment(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.Feuilleton
I actually implemented a WebView subclass that does this using the method described in this answer. igisolatedcookiewebview.googlecode.com (New BSD license).Chrisse
@Chrisse 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.Oxyhydrogen
This solution doesn't handle correctly cookies set by js document.cookie, does it?Lomond
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/ADCookieIsolatedWebViewLanna
@Kevin Ballard hey. can help me for swift 3.0 in Webview with an example with including your explanation.Balata
@ronakpatel At this point I'd recommend using WKWebView, which has a websiteDataStore property that you can set to a .nonPersistent() store, which won't share cookies (or persist them, but if you're not sharing cookies I assume you aren't persisting them either).Jacobine
@Kevin Ballard, i use this code already first Webview to second Webview its working but when I go back first view controller at that time. show same as SecondWebview .. and my source code link its:-#47591516 and your ans code implement in my code:-#38712149 of anwser by Roman ErmolovBalata
H
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?

Has answered 13/12, 2008 at 11:49 Comment(0)
M
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.

Microsurgery answered 12/12, 2008 at 22:2 Comment(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.Feuilleton
H
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.

Has answered 22/12, 2008 at 16:48 Comment(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. sighFeuilleton

© 2022 - 2024 — McMap. All rights reserved.