NSURLCache does not work offline with UIWebVIew - ios

I am trying to load UIWebView Offline with data from NSURLCache.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// setup cache
int cacheSizeMemory = 4*1024*1024; // 4MB
int cacheSizeDisk = 32*1024*1024; // 32MB
NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:#"nsurlcache"];
[NSURLCache setSharedURLCache:urlCache];
}
-(void)loadWebView{
request = [NSURLRequest requestWithURL:myURL cachePolicy: NSURLRequestReturnCacheDataElseLoad timeoutInterval:1000];
[self.webView loadRequest:request];
}
When I try to load UIWebView offline it calls didFailWithError instead of connectionDidFinishLoading. My Cache directory however contains offline data and resources like css, js files but UIWebView is not able to access it.
I have confirmed this by overriding the following method:
-(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
NSCachedURLResponse *response = [super cachedResponseForRequest:request];
if([response.data length]> 0){
// this is called for all css , js script files
NSLog(#"Returning data from cache");
}
return response;
}
Moreover, the data is being returned from cache UIWebview fails to load it.

Related

Rapidly increasing memory due to load url in UIWebview ios

I am loading URL in UIWebview and it is working fine.But during loading of web request it is consuming memory too much.Whenever i load some URL in UIWebview memory is increased from 30mb to 95mb and based on each link clicked in UIWebview it is still increasing and reach to 180mb and so on.I used some code to remove memoryin UIWebview.But there is no benefit.I have done memory management by Analyze in Xcode but there are no leaks and i also check allocations and leaks.It is just creating one instance of UIWebview.
This is my code:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
int cacheSizeMemory = 4*1024*1024; // 4MB
int cacheSizeDisk = 32*1024*1024; // 32MB
NSURLCache *sharedCache = [[[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:#"nsurlcache"] autorelease];
[NSURLCache setSharedURLCache:sharedCache];
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
ViewController.h
#property(nonatomic,strong)UIWebview *webview;
ViewController.m
#synthesize webview;
-(void)viewdidLoad
{
NSString *urlString = #"some url";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webview=[uiwebview alloc]init];
[self.webview loadRequest:request];
}
/*webview delegates
-(void)viewWillDisappear:(Bool)animated
{
self.webview=nil;
[self.webview removefromSuperView]
[self.webview loadHtmlString:#"" baseUrl:nil]
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
I am facing this issue for few days.Please give me some alternative or method to remove UIWebviewmemory.
In viewWillDisapear You are setting your WebView reference to nil so the removing in the next line fails.
Try setting your reference to nil after you completed all cleanup around the WebView, i.e. in the last line of the method.
You probably also need to remove the delegate of the WebView (before nilling).
As dogsgod is pointed out the way OP release memory is not proper, but
there are other things which should mention for this type of question.
If you are developing new application I suggest you to use ARC for your project
If its not possible to implement ARC then follow below advice
In viewdidLoad() you are allocating object that's good but its better if you first check that is object is already allocated of not (as it may change that it is allocated before pushing it into navigation).
Sample Code
if(self.webview!=nil)
{
self.webview = nil; // release memory
}
// Code for allocating memory.
If you alloc object in viewDidLoad() then must be deallco in dealloc() method.
NOTE:
When you assign any object nil, it means now object is not point to the old location and you have no control over that object, so calling some method like [self.webview removefromSuperView] after assigning nil to webview it have no effect.
Extra note:
Please read this document for knowledge about methods like viewDidload,viewWillAppear ... etc.

NSURLCache not working after iOS app termination

I am building an app in which I am implementing offline mode as well. For this I used NSURLCache mechanism. When the app is in foreground then the cache mechanism works perfectly even if the device goes in offline mode. The problem comes when I quit the app and then open it in offline mode. This time NSCachedURLResponse is returning nil for the particular request.
Below is the code I am using for this:
Custom Cache Creation:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:1024*1024 diskCapacity:1024*1024*5 diskPath:nil];
[NSURLCache setSharedURLCache:urlCache];
}
Calling server and cached object:
-(void)serverCall
{
NSURLCache *urlCache = [NSURLCache sharedURLCache];
NSString *urlString = nil;
NSHTTPURLResponse *response = nil;
NSError *error = nil;
urlString = [NSString stringWithFormat:#"%#%#",BASE_URL,url];
urlString = [urlString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *finalUrl = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:finalUrl];
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
NSData *data = nil;
if ([self checkInternetConnection])
{
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data];
[urlCache storeCachedResponse:cachedResponse forRequest:request];
}
else
{
NSCachedURLResponse *cachedResponse = [urlCache cachedResponseForRequest:request];
response = cachedResponse.response;
data = cachedResponse.data;
}
}
I can see that caches.db in library path of application the response is saved. But when i am trying to read it in Offline mode (after app is killed from background), the cached response is coming nil. I have gone through following links (and many more) but couldn't find the solution of my problem.
NSURLCache Problem with cache response
How to use NSURLCache to return cached API responses when offline (iOS App)
http://twobitlabs.com/2012/01/ios-ipad-iphone-nsurlcache-uiwebview-memory-utilization/
http://petersteinberger.com/blog/2012/nsurlcache-uses-a-disk-cache-as-of-ios5/
Bypassing http response header Cache-Control: how to set cache expiration?
I have checked the http header fields and cached header in the api response and the problem is not from server end.
I have also tried providing a path in diskPath param while creating the custom cache but then it doesn't even load the cached object while the app is in foreground and internet disconnects. I have also changed the expiration date but the working is still same.
I have tried using SDURLCache but I am facing the similar problem. However I have successfully achieved this with ASIHTTPRequest but I don't want to use it as I have written all the server operations once already and I have to change every request with ASIHTTPRequest method.
Please help me find a solution for this.
Thanks in advance....

NSURLCache not caching url

I create the url like so, which is part of a function that is called in a background thread.
NSURL *url = [NSURL URLWithString:urlString];
NSData *pageData = [NSData dataWithContentsOfURL:url];
And when I try to cache it
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
diskCapacity:20 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
}
However, if I open the app, close it, and try opening it again, the app takes the same amount of time to load and there is no noticeable improvement in speed. I'm wondering if there is an iOS limitation with caching, or if I have implemented the code incorrectly.
Are you including the proper e-tag in your request? Is your server returning a 200, or a 304?
In order to know that the cached data can be used, the server needs to indicate that it hasn't changed.

iOS Memory issue (ARC) when downloading and saving large ammount of images from server

The following code downloads 700+ images from a server with varying sizes, the issue here is that memory (even with ARC) is never released and eventually a memory warning appears followed by the application exiting. I've tried #autoreleasepool in this method and that didn't seem to work. Also I've tried stopping the for loop at different locations to see if memory is released after it finished, but it isn't.
This method is called inside a for loop and receives the image url and short name. It has been tried in a background thread and main thread with the same results (memory wise).
-(void)saveImage:(NSString*)image name:(NSString*)imageName{
int k = 0;
for (int j = 0; j < [imageName length]; j++) {
if ([imageName characterAtIndex:j] == '/') {
k = j;
}
}if (k != 0) {
imageName = [imageName substringFromIndex:k+1];
}
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#", imageName]];
if ([fileManager fileExistsAtPath:fullPath]) {
[fileManager removeItemAtPath:fullPath error:nil];
}
NSURL *url = [NSURL URLWithString:image];
NSData *data = [[NSData alloc]initWithContentsOfURL:url];
NSLog(#"Saved: %d:%#", [fileManager createFileAtPath:fullPath contents:data attributes:nil], url);
data = nil;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
int cacheSizeMemory = 4*1024*1024; // 4MB
int cacheSizeDisk = 32*1024*1024; // 32MB
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:#"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
NSLog(#"mem warning, clearing cache");
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
Based on your screenshot, I think the issue is with the NSURL caching rather than the actual NSData objects. Can you try the following:
In your Application Delegate, setup an initial URL cache:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Do initial setup
int cacheSizeMemory = 16*1024*1024; // 16MB
int cacheSizeDisk = 32*1024*1024; // 32MB
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:#"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];
// Finish the rest of your didFinishLaunchingWithOptions and head into the app proper
}
Add the following to your Application Delegate:
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
A cache file will be created:"Library/Caches/your_app_id/nsurlcache"
The link to the Apple example is here: URL Cache
Code not tested but this (or something similar) should sort your problem + plus you can experiment with the cache sizes.
Can you post another screenshot of Allocations in action with this code? I would expect to see memory use stop growing and flatten out.
"dataWithContentsOfURL" returns an autoreleased NSData object and that doesn't normally get released until the end of the run loop or the end of the method, so it's no wonder you're filling up memory pretty quickly.
Change it to an explicit "initWithContentsOfURL" method and then force a release by doing "data = nil;" when you are absolutely done with the image data.

Clearing UIWebview cache

I have used UIWebview to load a web page using loadRequest: method, when I leave that scene I call [self.webView stopLoading]; and release the webView.
In activity monitor on first launch i have seen that the real memory increased by 4MB, and on multiple launches/loading the real memory doesn't increase. It is increasing only once.
I have checked the retain count of webview. It is proper i.e., 0. I think UIWebView is caching some data. How do I avoid caching or remove cached data? Or is there another reason for this?
I actually think it may retain cached information when you close out the UIWebView. I've tried removing a UIWebView from my UIViewController, releasing it, then creating a new one. The new one remembered exactly where I was at when I went back to an address without having to reload everything (it remembered my previous UIWebView was logged in).
So a couple of suggestions:
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];
This would remove a cached response for a specific request. There is also a call that will remove all cached responses for all requests ran on the UIWebView:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
After that, you can try deleting any associated cookies with the UIWebView:
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
Swift 3:
// Remove all cache
URLCache.shared.removeAllCachedResponses()
// Delete any associated cookies
if let cookies = HTTPCookieStorage.shared.cookies {
for cookie in cookies {
HTTPCookieStorage.shared.deleteCookie(cookie)
}
}
Don't disable caching completely, it'll hurt your app performance and it's unnecessary. The important thing is to explicitly configure the cache at app startup and purge it when necessary.
So in application:DidFinishLaunchingWithOptions: configure the cache limits as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
int cacheSizeMemory = 4*1024*1024; // 4MB
int cacheSizeDisk = 32*1024*1024; // 32MB
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:#"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];
// ... other launching code
}
Once you have it properly configured, then when you need to purge the cache (for example in applicationDidReceiveMemoryWarning or when you close a UIWebView) just do:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
and you'll see the memory is recovered. I blogged about this issue here: http://twobitlabs.com/2012/01/ios-ipad-iphone-nsurlcache-uiwebview-memory-utilization/
You can disable the caching by doing the following:
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
ARC:
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
Swift 3.
// Remove all cache
URLCache.shared.removeAllCachedResponses()
// Delete any associated cookies
if let cookies = HTTPCookieStorage.shared.cookies {
for cookie in cookies {
HTTPCookieStorage.shared.deleteCookie(cookie)
}
}
I could not change the code, so I needed command line for testing purpose and figured this could help someone:
Application specific caches are stored in ~/Library/Caches/<bundle-identifier-of-your-app>, so simply remove as below and re-open your application
rm -rf ~/Library/Caches/com.mycompany.appname/
My educated guess is that the memory use you are seeing is not from the page content, but rather from loading UIWebView and all of it's supporting WebKit libraries. I love the UIWebView control, but it is a 'heavy' control that pulls in a very large block of code.
This code is a large sub-set of the iOS Safari browser, and likely initializes a large body of static structures.
After various attempt, only this works well for me (under ios 8):
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:1 diskCapacity:1 diskPath:nil];
[NSURLCache setSharedURLCache:cache];
For swift 2.0:
let cacheSizeMemory = 4*1024*1024; // 4MB
let cacheSizeDisk = 32*1024*1024; // 32MB
let sharedCache = NSURLCache(memoryCapacity: cacheSizeMemory, diskCapacity: cacheSizeDisk, diskPath: "nsurlcache")
NSURLCache.setSharedURLCache(sharedCache)
I am loading html pages from Documents and if they have the same name of css file UIWebView it seem it does not throw the previous css rules. Maybe because they have the same URL or something.
I tried this:
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
I tried this :
[[NSURLCache sharedURLCache] removeAllCachedResponses];
I am loading the initial page with:
NSURLRequest *appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
It refuses to throw its cached data! Frustrating!
I am doing this inside a PhoneGap (Cordova) application. I have not tried it in isolated UIWebView.
Update1: I have found this.
Changing the html files, though seems very messy.

Resources