Questions tagged [nsurlcache]

NSURLCache is a class that is used in ios developing for caching the responses of the URL. It is available in OS X v10.2 with Safari 1.0 installed, available in OS X v10.2.7 and later .

The NSURLCache class implements the caching of responses to URL load requests by mapping NSURLRequest objects to NSCachedURLResponse objects.

It provides a composite in-memory and on-disk cache, and lets you manipulate the sizes of both the in-memory and on-disk portions. You can also control the path where cache data is stored persistently.

In iOS, the on-disk cache may be purged when the system runs low on disk space, but only when your app is not running.

To create a new cache object:

   - (instancetype)initWithMemoryCapacity:(NSUInteger)memoryCapacity
                      diskCapacity:(NSUInteger)diskCapacity
                          diskPath:(NSString *)path;

Sample Example for creation of new cache:

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   // Set app-wide shared cache (first number is megabyte value)
  NSUInteger cacheSizeMemory = 500*1024*1024; // 500 MB
  NSUInteger cacheSizeDisk = 500*1024*1024; // 500 MB
  NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
 [NSURLCache setSharedURLCache:sharedCache];

}

Question related with this can be tagged using , or tags

Read more

267 questions
3
votes
1 answer

NSURLSession's NSURLCache not working as expected

I'm seeing weird behaviors when using NSURLCache and exhausted all options but posting here... so here it is: I'm trying to implement NSURLCache as underlying caching mechanism in combination with NSURLSession. And using Charles and simple println()…
opfeffer
  • 603
  • 5
  • 19
3
votes
1 answer

ios NSURLSession (default configuration) does not cache requests

I've configured the session: - (void)createSession { NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; sessionConfig.requestCachePolicy = NSURLRequestReturnCacheDataElseLoad; _session…
3
votes
1 answer

NSURLCache is emptied when app is closed

i'm trying to implement a NSURLCache with AFHTTPRequestOperation, this is what i have done: AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSURLCache *URLCache =…
Piero
  • 9,173
  • 18
  • 90
  • 160
3
votes
0 answers

How do I tell if an NSURLSessionDataTask serves its response from a cache?

How can I tell if my NSURLSessionDataTask has satisfied its request from the cache, rather than by going over the network? (I don't think it should matter, but I'm creating the task via the default [NSURLSession sharedSession] and not setting up…
Robert Atkins
  • 23,528
  • 15
  • 68
  • 97
3
votes
1 answer

UIWebView NSURLRequestReloadIgnoringLocalCacheData doesn't actually ignore the cache

I have a UIWebView object, with the caching-policy specified as: NSURLRequestReloadIgnoringLocalCacheData This should ignore whatever objects are in the local cache and retrieve the latest version of a site from the web. However, after the first…
dodeskjeggen
  • 166
  • 2
  • 8
3
votes
3 answers

NSURLCache Problem with cache response

I'm writing an iPhone application, one of it's tabs is a twitter feed, i'm parsing twitter xml and putting it nicely inside a table view. In case there is no internet connection I would like to show cache results of the last time we had internet…
djTeller
  • 515
  • 2
  • 5
  • 14
3
votes
4 answers

Mutable NSHTTPURLResponse or NSURLResponse

I need to modify response headers in an NSURLResponse. Is this possible?
Arlen Anderson
  • 2,486
  • 2
  • 25
  • 36
3
votes
1 answer

Download and cache entire web page on ios

I need to download an entire webPage and store it in app's documents directory and load it from the cache the next time user visits. No matter how much I search, I always end up with ASIWebPageRequest..!! even though, it works, its very old and…
akshaynhegde
  • 1,938
  • 3
  • 17
  • 36
3
votes
1 answer

Intercepting spawned URL requests from UIWebView

I'm trying to cache specified web pages to disk by NSURLProtocol,now I can only get the first URL request, but the UIWebView spawns further URL requests for loading graphics. So how can I Intercept all the requests spawned by UIWebView from the…
Suge
  • 2,808
  • 3
  • 48
  • 79
3
votes
1 answer

What happens if NSURLCache is full?

I'm using default NSURLCache to cache images in my iPhone app. What will happen if the cache is full and i'll try to cache another image? Will it not cache the image? or it will be replaced with the oldest image cached? Thanks alot
Patz
  • 121
  • 6
3
votes
0 answers

iOS NSURLCache / NSURLConnection 304 + max-age

Just out of curiosity does NSURLCache just completely ignore the max-age cache control header on 304 responses? I have a system setup using etags and max-age, that in theory should only send a request to the web service every 10 seconds based on the…
Holyprin
  • 348
  • 1
  • 9
3
votes
1 answer

Clearing the url cache on memory warning in iOS

Is it a good practice to clear the shared NSURLCache when receiving a memory warning? Something like this: - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { [[NSURLCache sharedURLCache] removeAllCachedResponses]; } Am I…
hpique
  • 119,096
  • 131
  • 338
  • 476
2
votes
1 answer

How to set the Caching Model (for NSURLCache & UIWebView) in iOS?

I'm wondering if this is a bug in the iOS framework. The NSURLCache setMemoryCapacity: method is being called with 0 and making it stop caching. I'm seeing this happen when presenting a UIWebView. I'm using SDURLCache and overrode…
Dad
  • 6,388
  • 2
  • 28
  • 34
2
votes
3 answers

WKWebView doesn't use URLCache

I have an application which use UIWebView to show some local content. I Also have a custom URLCache to intercept requests and replace them with local content: class LocalWebViewCache: URLCache { open override func cachedResponse(for request:…
mehdok
  • 1,499
  • 4
  • 29
  • 54
2
votes
0 answers

swift URLCache after IOS 12 update

I extended URLCache to implement offline browsing of a web site in IOS app using UIWebView. Before IOS 12 every thing was working fine. cachedResponse, getCachedResponse, storeCachedResponse and storeCachedResponse are called as intended, but after…
Nizar Ahmed
  • 180
  • 9