views:

675

answers:

2

How do existing apps implement this feature???

Can I store cookie only for certain sites, and only inside my app? It's my understand that the web view stores cookies in shared mode...so that they are shared with Safari and other apps that use UIWebView.

+3  A: 

If your talking about Private Browsing the UIWebView actually does not store history after the app is closed (only temporary for going back and forth). Instead you would have to implement storing history yourself, so it would be automatically Private Browsing.

Short answer: Don't do anything. Its already in Private Browsing mode.

EDIT: For handling cache check out this method:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse

And make cashedResponse return nil.

thyrgle
mmm...ok about the history...but what about cached images, and cookies? I think at least cookies are preserved, and probably images too...no?
devguy
Ok I updated my answer :).
thyrgle
thanks thyrgle...that sounds like a good response but...where/how do I implement it in the UIWebView? Or should I imeplement in a subclass for NSURLCache as shown here: http://www.icab.de/blog/2009/08/18/url-filtering-with-uiwebview-on-the-iphone/Is it any useful to implement this, is the UIWebView OS doesn't store a cache?
devguy
+1  A: 

According to the NSHTTPCookieStorage docs, cookies are not shared between applications:

iPhone OS Note: Cookies are not shared among applications in iPhone OS.

So it seems like they should be "private" by default. You can also use the [NSHTTPCookieStorage sharedHTTPCookieStorage] object to set the cookie storage policy to not store cookies at all, or you could use the deleteCookie: method to clean up after yourself if you needed to.

As for other content that is loaded by your UIWebview, when you create the NSURLRequest that is loaded by your webview, you can set a cache policy that controls if the content will be cached. For example:

NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: url]
                                          cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                       timeoutInterval: 60.0]
[webView loadRequest: request];

NSURLRequestReloadIgnoringLocalAndRemoteCacheData tells the request to ignore the cache and load the request from the network. I'm not sure if it also prevents the response from the network from being cached, but to be sure, you could alway remove it from the cache yourself:

[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];
Jason Jenkins