0

I have a website which can be used offline too. I have created an app which use UIWebView and I said that my UIWebView should cache this site with the following code

[self setRequestObj:[NSURLRequest requestWithURL:loadUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]];

If I started my app the first time with internet connection my console logged

2016-08-24 15:37:01.713 BrowserApp[1991:345138] applicationDidBecomeActive!
2016-08-24 15:37:01.955 BrowserApp[1991:345138] NSURLRequestUseProtocolCachePolicy
2016-08-24 15:37:22.003 BrowserApp[1991:345138] applicationDidEnterBackground!
2016-08-24 15:37:22.559 BrowserApp[1991:345138] applicationWillEnterForeground!
2016-08-24 15:37:23.075 BrowserApp[1991:345138] applicationDidBecomeActive!
2016-08-24 15:37:37.069 BrowserApp[1991:345138] applicationDidEnterBackground!
2016-08-24 15:37:37.073 BrowserApp[1991:345138] applicationWillTerminate!

I see that every method is called in the file AppDelegate.m. I disconnect my phone from the internet, launch my app again (wasn't in the background like my console told) and nothing happens. I only get a white website.

Why the website wouldn't be displayed on my screen although I set to cache it?

Premox
  • 183
  • 3
  • 23

2 Answers2

1

It may be that the page you have loaded has an expiration date (it may be set to expire immediately). Try with the cahce policy set as NSURLRequestReturnCacheDataElseLoad (although that might not be good in the long term, because it will never reload).

Alternatively, (more probably) the response headers from the website may contain a cache-control header. Regardless of what you say, the page can still tell NSURLRequest that it should not be cached.

To investigate, you could set a breakpoint or put some logging in your code where you handle did receive response. Cast the NSURLResponse to an NSHTTPURLResponse and then look at the headers using allHeaderFields.

ncke
  • 969
  • 7
  • 12
1

Seems like a bug with URLCache. When I kill the app, put the phone in airplane mode and relaunch the app, the WebViewDelegate's didFailLoadWithError gets called even though the response is cached.

Also the URLCache.shared.cachedResponse(for: urlRequest) doesn't return a CachedURLResponse.

I also checked the Cache.db and it was containing the saved responses and assets.

Wierd!

Sameer Bhide
  • 163
  • 7