iOS Developer Library

Developer

Foundation Framework Reference NSHTTPCookieStorage Class Reference

Options
Deployment Target:

On This Page
Language:

NSHTTPCookieStorage

NSHTTPCookieStorage implements a singleton object (shared instance) that manages storage of cookies. Each cookie is represented by an instance of the NSHTTPCookie class. As a rule, cookies are shared among all applications and are kept in sync across process boundaries. Session cookies (where the cookie object’s isSessionOnly method returns YEStrue) are local to a single process and are not shared. More...

Inheritance


Conforms To


Import Statement


import Foundation @import Foundation;

Availability


Available in iOS 2.0 and later.
  • initWithStorageLocation: initWithStorageLocation: Available in iOS 4.0 through iOS 4.3

    Returns an initialized NSHTTPCookieStorage object with a given file system location to store cookie information on disk.

    Declaration

    Objective-C

    - (id)initWithStorageLocation:(NSURL *)storageFileURL

    Parameters

    storageFileURL

    A file:// URL that indicates the file to use for cookie storage.

    Return Value

    An initialized NSHTTPCookieStorage object that stores its cookie information at storageFileURL.

    Import Statement

    Availability

    Available in iOS 4.0 through iOS 4.3.

  • Returns the shared cookie storage instance.

    Declaration

    Swift

    class func sharedHTTPCookieStorage() -> NSHTTPCookieStorage

    Objective-C

    + (NSHTTPCookieStorage *)sharedHTTPCookieStorage

    Return Value

    The shared cookie storage instance.

    Import Statement

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Deletes the specified cookie from the cookie storage.

    Declaration

    Swift

    func deleteCookie(_ aCookie: NSHTTPCookie)

    Objective-C

    - (void)deleteCookie:(NSHTTPCookie *)aCookie

    Parameters

    aCookie

    The cookie to delete.

    Import Statement

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Stores a specified cookie in the cookie storage if the cookie accept policy permits.

    Declaration

    Swift

    func setCookie(_ aCookie: NSHTTPCookie)

    Objective-C

    - (void)setCookie:(NSHTTPCookie *)aCookie

    Parameters

    aCookie

    The cookie to store.

    Discussion

    The cookie replaces an existing cookie with the same name, domain, and path, if one exists in the cookie storage. This method accepts the cookie only if the receiver’s cookie accept policy is NSHTTPCookieAcceptPolicyAlways or NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain. The cookie is ignored if the receiver’s cookie accept policy is NSHTTPCookieAcceptPolicyNever.

    Import Statement

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Adds an array of cookies to the receiver if the receiver’s cookie acceptance policy permits.

    Declaration

    Swift

    func setCookies(_ cookies: [AnyObject], forURL theURL: NSURL?, mainDocumentURL mainDocumentURL: NSURL?)

    Objective-C

    - (void)setCookies:(NSArray *)cookies forURL:(NSURL *)theURL mainDocumentURL:(NSURL *)mainDocumentURL

    Parameters

    cookies

    The cookies to add.

    theURL

    The URL associated with the added cookies.

    mainDocumentURL

    The URL of the main HTML document for the top-level frame, if known. Can be nil. This URL is used to determine if the cookie should be accepted if the cookie accept policy is NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain.

    Discussion

    The cookies will replace existing cookies with the same name, domain, and path, if one exists in the cookie storage. The cookie will be ignored if the receiver's cookie accept policy is NSHTTPCookieAcceptPolicyNever.

    To store cookies from a set of response headers, an application can use cookiesWithResponseHeaderFields:forURL: passing a header field dictionary and then use this method to store the resulting cookies in accordance with the receiver’s cookie acceptance policy.

    Import Statement

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • cookies cookies Property

    The cookie storage’s cookies. (read-only)

    Declaration

    Swift

    var cookies: [AnyObject]? { get }

    Objective-C

    @property(readonly, copy) NSArray *cookies

    Discussion

    If you want to sort the cookie storage’s cookies, you should use the sortedCookiesUsingDescriptors: method instead of sorting the result of this method.

    Import Statement

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Returns all the cookie storage’s cookies that are sent to a specified URL.

    Declaration

    Swift

    func cookiesForURL(_ theURL: NSURL) -> [AnyObject]?

    Objective-C

    - (NSArray *)cookiesForURL:(NSURL *)theURL

    Parameters

    theURL

    The URL to filter on.

    Return Value

    An array of cookies whose URL matches the provided URL.

    Discussion

    An application can use NSHTTPCookie method requestHeaderFieldsWithCookies: to turn this array into a set of header fields to add to an NSMutableURLRequest object.

    Import Statement

    import Foundation

    Availability

    Available in iOS 2.0 and later.

    See Also

    – cookies

  • Returns all of the cookie storage’s cookies, sorted according to a given set of sort descriptors.

    Declaration

    Swift

    func sortedCookiesUsingDescriptors(_ sortOrder: [AnyObject]) -> [AnyObject]

    Objective-C

    - (NSArray *)sortedCookiesUsingDescriptors:(NSArray *)sortOrder

    Parameters

    sortOrder

    The sort descriptors to use for sorting, as an array of NSSortDescriptor objects.

    Return Value

    The cookie storage’s cookies, sorted according to sortOrder, as an array of NSHTTPCookie objects.

    Import Statement

    import Foundation

    Availability

    Available in iOS 5.0 and later.

Data Types

  • NSHTTPCookieAcceptPolicy specifies the cookie acceptance policies implemented by the NSHTTPCookieStorage class.

    Declaration

    Swift

    enum NSHTTPCookieAcceptPolicy : UInt { case Always case Never case OnlyFromMainDocumentDomain }

    Objective-C

    typedef enum { NSHTTPCookieAcceptPolicyAlways, NSHTTPCookieAcceptPolicyNever, NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain } NSHTTPCookieAcceptPolicy;

    Constants

    • Always

      NSHTTPCookieAcceptPolicyAlways

      Accept all cookies. This is the default cookie accept policy.

      Available in iOS 2.0 and later.

    • Never

      NSHTTPCookieAcceptPolicyNever

      Reject all cookies.

      Available in iOS 2.0 and later.

    • OnlyFromMainDocumentDomain

      NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain

      Accept cookies only from the main document domain.

      Available in iOS 2.0 and later.

    Import Statement

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • This notification is posted when the cookies stored in the NSHTTPCookieStorage instance have changed.

    In OS X, cookies are shared among applications, meaning this notification can be sent in response to another application’s actions. Cookies are not shared among applications in iOS.

    The notification object is the NSHTTPCookieStorage instance. This notification does not contain a userInfo dictionary.

    Import Statement

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • This notification is posted when the acceptance policy of the NSHTTPCookieStorage instance has changed.

    In OS X, cookies are shared among applications, meaning this notification can be sent in response to another application’s actions. Cookies are not shared among applications in iOS.

    The notification object is the NSHTTPCookieStorage instance. This notification does not contain a userInfo dictionary.

    Import Statement

    import Foundation

    Availability

    Available in iOS 2.0 and later.