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
6
votes
1 answer

What is the better way to encrypt NSURLCache?

I want to encrypt/decrypt all cached data from a NSURLSession using AES256. I'm new using Alamofire but I think it is possible to do it without involving the library itself. I don't know exactly what is the most seamless way to encrypt the data…
emenegro
  • 6,901
  • 10
  • 45
  • 68
6
votes
1 answer

WKWebView and NSURLCache to serve local content

My app loads a very big webapp inside a UIWebView. I have written a NSURLCache extension "LocalCache". This LocalCache extension intercepts my webapp loading, and serves all requested files from a local app bundle that is encrypted. This logic is…
rupps
  • 9,712
  • 4
  • 55
  • 95
6
votes
1 answer

Shared NSURLCache and UIWebView on iOS 8

In iOS 7, I was able to set a shared URL cache to a subclass of NSURLCache and any UIWebViews I created would automatically use that shared cache for each request. // Set the URL cache and leave it set permanently ExampleURLCache *cache =…
Jon Willis
  • 6,993
  • 4
  • 43
  • 51
6
votes
3 answers

How do I invalidate iOS's cache for a particular URL?

Using NSURLSession's default caching, how do I invalidate the cache for a particular URL? I note NSURLCache's removeCachedResponseForRequest: method, but that takes an NSURLRequest object, which I don't have for the original request. Do I need to…
Robert Atkins
  • 23,528
  • 15
  • 68
  • 97
6
votes
3 answers

Is NSURLCache thread safe?

I know NSCache is thread safe, however I can't find out any document mentioned that NSURLCache is thread safe.
Pei
  • 460
  • 4
  • 21
6
votes
1 answer

Preventing UIWebView to allocate memory indefinitely (NSURLCache apparently not working)

I have been trying to understand how the UIWebView cache works. Since my goal is to be able to manage the memory allocated by the UIWebView (at least, as much as possible), to avoid memory raising indefinitely and the App getting killed because of…
veducm
  • 5,933
  • 2
  • 34
  • 40
6
votes
1 answer

Does NSURLCache remove expired cached responses?

Apple has supported disk caching as of iOS 5.0. I was using a home-rolled solution before, but I'm testing NSURLCache in hopes of finally using it since I've seen strange behavior in the past. One of the more perplexing issues I'm having is that…
goldierox
  • 1,085
  • 1
  • 8
  • 23
5
votes
1 answer

Use NSURLConnection cache when device is offline

In an NSOperation subclass, I am using the following code to download an xml-file from our server, and then later parse it: NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] …
vtim
  • 101
  • 6
5
votes
1 answer

URLCache (iOS). storeCachedResponse works asynchronously. How to catch the completion?

Just discovered that the function storeCachedResponse(_ cachedResponse: CachedURLResponse, for request: URLRequest) works asynchronously. That is, the result is not returned immediately after execution. I did not find a description of this in the…
5
votes
1 answer

iOS URLCache caching when it shouldn't (IMHO)

Does anyone know why this request is being cached? I'm using an unmodified .default URLSessionConfiguration. The response headers are: (from Charles, confirmed from debugging the response in the data tasks's completion block) { …
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
5
votes
0 answers

Share an instance of NSURLCache between an app and extension in a Swift framework

I want to share an instance of NSURLCache between my app and an extension so tried instantiating it from a Swift framework. Is there any way to spawn the URL cache on a shared (container) disk space?
julien_c
  • 4,942
  • 5
  • 39
  • 54
5
votes
1 answer

When I use NSCachedURLResponse,I got "301PermMove"

when I test NSURLCache,I got "301PermMove",this is my code -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSURL *url = [NSURL URLWithString:@"https://www.github.com"]; NSURLRequest *urlRequest = [[NSURLRequest…
hmxxxhhh
  • 59
  • 4
5
votes
1 answer

How to use cache policy NSURLRequestReloadRevalidatingCacheData

The links below iOS - Download file only if modified (NSURL & NSData) http://nshipster.com/nsurlcache/ mentions that NSURLRequestReloadRevalidatingCacheData is not yet implemented in iOS 7. Is this implemented in iOS 12? If it is, can you tell me…
Ted
  • 22,696
  • 11
  • 95
  • 109
5
votes
0 answers

NSURLCache does not work offline with UIWebVIew

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…
Kunal Balani
  • 4,739
  • 4
  • 36
  • 73
5
votes
1 answer

Unit tests with NSURLSession

I'd like to write unit tests for methods that use the shared NSURLSession, and in particular, NSURLSessionDataTask to download data. Basically, I would like the target methods to receive mock responses/data without requiring an internet…
hpique
  • 119,096
  • 131
  • 338
  • 476
1 2
3
17 18