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

cachedResponseForRequest won't be called after loading the same URL

I subclassed NSURLCache, and overwrote cachedResponseForRequest, and changed the shared cache to my own cache object, to intercept every url request made by a UIWebView. It works perfectly at the first time, when the URL loaded into the UIWebView,…
Kompi
  • 484
  • 4
  • 18
0
votes
0 answers

URLCache not caching 404 response

My server is sending proper cache headers, however URLSession will not cache the response. Is there any way to make it cache, although it is a 404? HTTP/1.1 404 Not Found Content-Type: application/json; charset=utf-8 Cache-Control: public,…
Mick
  • 954
  • 7
  • 17
0
votes
1 answer

NSURLCACHE policy to update cache data if website is reachable and so always load cache

I'm totally new to swift and iOS development. I'm trying to do a webview app and i need to update the cache because the website can change but i also need to be able to access it while offline. Is there any premade cache policy i can use in order to…
0
votes
1 answer

Alamofire Cache not found

I have been trying to setup the cache for Alamofire 5.0 by doing the configuration below: private func buildURLCache() -> URLCache { let capacity = 50 * 1024 * 1024 // MBs #if targetEnvironment(macCatalyst) return URLCache(memoryCapacity:…
Pedro Paulo Amorim
  • 1,838
  • 2
  • 27
  • 50
0
votes
1 answer

Server access possible with wrong credentials after initial successful authentication

I login into a server using credentials, which are passed inside the request parameter like this: https://server.i.connect.to/login&username=user&password=pass. All works fine, if credentials are correct. If I then change credentials into something…
geohei
  • 696
  • 4
  • 15
0
votes
0 answers

URLCache and eviction control

Just want to share my indignation. Maybe someone will advise something. There is a class URLCache. As I understand it, this class does not provide any tools to perform actions when an object is about to be evicted or removed from the cache. No…
0
votes
1 answer

NSURLCaching with dataTask fails to read Cache back with NSURLRequest when offline

I am very confused by iOS caching system. It should be straightforward but it not (for me). I am writing an database App which allows for external web access to a limited range of additional reference material in the form of web pages. Sometimes a…
0
votes
1 answer

How to override Alamofire SessionManager to modify response in order to add headers?

So, I'm using Alamofire to make some HTTP requests and I'm trying to use the caching policy but the server doesn't have the "Cache-Control" flag in its headers. So I want to add this flag using the session manager delegate method…
Nathan Barreto
  • 365
  • 2
  • 21
0
votes
1 answer

NSURLCache cachedResponseForRequest getting call twice

In my code i had override NSURLCache cachedResponseForRequest to send the cached response in case if downloaded already. But for some reason cachedResponseForRequest getting called twice. Could anybody know the reason? Is this a default behaviour?
Mini2008
  • 627
  • 1
  • 6
  • 12
0
votes
1 answer

Caching downloaded images: FileManager / CachePolicy / URLCache / NSCache?

I'm need to implement the common scenario of having a table view with N cells where each of those cells needs to download an image to be displayed within it. The service protocol to call to download the images could be either HTTP or HTTPS. I am…
AppsDev
  • 12,319
  • 23
  • 93
  • 186
0
votes
1 answer

What happens when URLCache limit is reached

I use URLCache to cache request response with a max capacity like this : let diskCapacity = 100 * 1024 * 1024 let memoryCapacity = 100 * 1024 * 1024 let cache = URLCache(memoryCapacity: memoryCapacity, diskCapacity: diskCapacity,…
Louis
  • 406
  • 6
  • 13
0
votes
1 answer

Caching AlamofireImage responses to disk

I need a little help with AlamofireImages. In the app, tapping on a table view cell will download and set an image using the built in ImageView extension, seen below: func sendImageRequest(imageView: UIImageView, item: CatalogItem, isLargeImage:…
Mystified
  • 137
  • 1
  • 9
0
votes
1 answer

NSURLCache cachedresponseforrequest no data

why the responseCache is nil? i'd run this post and really get the responseObject from cache. How can i get the responseCache? manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer…
0
votes
1 answer

How do I cache a file download request using Alamofire?

I have been trying to achieve caching of a file that I download from an url. File is about 5 MB in size. Below is the header that I obtain from that file url: Content-Type : application/x-zip-compressed Content-Length : 4088083 Expires : Sun, 24 Jun…
Bibek
  • 3,689
  • 3
  • 19
  • 28
0
votes
1 answer

shouldStartLoadWithRequest: not called for images

I have a UIWebView and I make it load an HTML string. The string contains http urls to images. The images get loaded, but shouldStartLoadWithRequest: method is not called for them. It is called just once, for about : blank. However all the image…
Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161