iOS Developer Library — Prerelease

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.

  • - 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.

    Availability

    Available in iOS 4.0 through iOS 4.3.

  • Deletes the specified cookie from the cookie storage.

    Declaration

    Swift

    func deleteCookie(_ cookie: NSHTTPCookie)

    Objective-C

    - (void)deleteCookie:(NSHTTPCookie * _Nonnull)aCookie

    Parameters

    aCookie

    The cookie to delete.

    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(_ cookie: NSHTTPCookie)

    Objective-C

    - (void)setCookie:(NSHTTPCookie * _Nonnull)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.

    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: [NSHTTPCookie], forURL URL: NSURL?, mainDocumentURL mainDocumentURL: NSURL?)

    Objective-C

    - (void)setCookies:(NSArray<NSHTTPCookie *> * _Nonnull)cookies forURL:(NSURL * _Nullable)theURL mainDocumentURL:(NSURL * _Nullable)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.

    Availability

    Available in iOS 2.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

    Objective-C

    @import Foundation;

    Swift

    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.

    Declaration

    Swift

    let NSHTTPCookieManagerCookiesChangedNotification: String

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    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.

    Declaration

    Swift

    let NSHTTPCookieManagerAcceptPolicyChangedNotification: String

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.