Mac Developer Library

Developer

Foundation Framework Reference NSURLCache Class Reference

Options
Deployment Target:

On This Page
Language:

NSURLCache

Inheritance


Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


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.

  • Returns the shared NSURLCache instance.

    Declaration

    Swift

    class func sharedURLCache() -> NSURLCache

    Objective-C

    + (NSURLCache *)sharedURLCache

    Return Value

    The shared NSURLCache instance.

    Discussion

    Applications that do not have special caching requirements or constraints should find the default shared cache instance acceptable. An application with more specific needs can create a custom NSURLCache object and set it as the shared cache instance using setSharedURLCache:. The application should do so before any calls to this method.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.2 with Safari 1.0 installed.

    Available in OS X v10.2.7 and later.

  • Sets the shared NSURLCache instance to a specified cache object.

    Declaration

    Swift

    class func setSharedURLCache(_ cache: NSURLCache)

    Objective-C

    + (void)setSharedURLCache:(NSURLCache *)cache

    Parameters

    cache

    The cache object to use as the shared cache object.

    Discussion

    An application that has special caching requirements or constraints should use this method to specify an NSURLCache instance with customized cache settings. The application should do so before any calls to the sharedURLCache method.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.2 with Safari 1.0 installed.

    Available in OS X v10.2.7 and later.

  • Initializes an NSURLCache object with the specified values.

    Declaration

    Swift

    init(memoryCapacity memoryCapacity: Int, diskCapacity diskCapacity: Int, diskPath path: String?)

    Objective-C

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

    Parameters

    memoryCapacity

    The memory capacity of the cache, in bytes.

    diskCapacity

    The disk capacity of the cache, in bytes.

    path

    In OS X, path is the location at which to store the on-disk cache.

    In iOS, path is the name of a subdirectory of the application’s default cache directory in which to store the on-disk cache (the subdirectory is created if it does not exist).

    Return Value

    The initialized NSURLCache object.

    Discussion

    The returned NSURLCache is backed by disk, so developers can be more liberal with space when choosing the capacity for this kind of cache. A disk cache measured in the tens of megabytes should be acceptable in most cases.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.2 with Safari 1.0 installed.

    Available in OS X v10.2.7 and later.

  • Returns the cached URL response in the cache for the specified URL request.

    Declaration

    Swift

    func cachedResponseForRequest(_ request: NSURLRequest) -> NSCachedURLResponse?

    Objective-C

    - (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request

    Parameters

    request

    The URL request whose cached response is desired.

    Return Value

    The cached URL response for request, or nil if no response has been cached.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.2 with Safari 1.0 installed.

    Available in OS X v10.2.7 and later.

  • Stores a cached URL response for a specified request

    Declaration

    Swift

    func storeCachedResponse(_ cachedResponse: NSCachedURLResponse, forRequest request: NSURLRequest)

    Objective-C

    - (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request

    Parameters

    cachedResponse

    The cached URL response to store.

    request

    The request for which the cached URL response is being stored.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.2 with Safari 1.0 installed.

    Available in OS X v10.2.7 and later.

  • Clears the receiver’s cache, removing all stored cached URL responses.

    Declaration

    Swift

    func removeAllCachedResponses()

    Objective-C

    - (void)removeAllCachedResponses

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.2 with Safari 1.0 installed.

    Available in OS X v10.2.7 and later.

  • Removes the cached URL response for a specified URL request.

    Declaration

    Swift

    func removeCachedResponseForRequest(_ request: NSURLRequest)

    Objective-C

    - (void)removeCachedResponseForRequest:(NSURLRequest *)request

    Parameters

    request

    The URL request whose cached URL response should be removed. If there is no corresponding cached URL response, no action is taken.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.2 with Safari 1.0 installed.

    Available in OS X v10.2.7 and later.

  • The current size of the receiver’s on-disk cache, in bytes. (read-only)

    Declaration

    Swift

    var currentDiskUsage: Int { get }

    Objective-C

    @property(readonly) NSUInteger currentDiskUsage

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.2 with Safari 1.0 installed.

    Available in OS X v10.2.7 and later.

  • The capacity of the receiver’s on-disk cache, in bytes.

    Declaration

    Swift

    var diskCapacity: Int

    Objective-C

    @property NSUInteger diskCapacity

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.2 with Safari 1.0 installed.

    Available in OS X v10.2.7 and later.

  • The current size of the receiver’s in-memory cache, in bytes. (read-only)

    Declaration

    Swift

    var currentMemoryUsage: Int { get }

    Objective-C

    @property(readonly) NSUInteger currentMemoryUsage

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.2 with Safari 1.0 installed.

    Available in OS X v10.2.7 and later.

  • The capacity of the receiver’s in-memory cache, in bytes.

    Declaration

    Swift

    var memoryCapacity: Int

    Objective-C

    @property NSUInteger memoryCapacity

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.2 with Safari 1.0 installed.

    Available in OS X v10.2.7 and later.