iOS Developer Library

Developer

Foundation Framework Reference NSHTTPCookie Class Reference

Options
Deployment Target:

On This Page
Language:

NSHTTPCookie

Inherits From


Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 2.0 and later

An NSHTTPCookie object represents an HTTP cookie. It is an immutable object, initialized from a dictionary containing the cookie attributes.

The NSHTTPCookie class supports two different cookie versions:

  • Version 0: This version refers to “traditional” or “old-style” cookies, the original cookie format defined by Netscape. The majority of cookies encountered are in this format.

  • Version 1: This version refers to cookies as defined in RFC 2965, HTTP State Management Mechanism.

  • Returns an array of NSHTTPCookie objects corresponding to the provided response header fields for the provided URL.

    Declaration

    Swift

    class func cookiesWithResponseHeaderFields(_ headerFields: [NSObject : AnyObject], forURL theURL: NSURL) -> [AnyObject]

    Objective-C

    + (NSArray *)cookiesWithResponseHeaderFields:(NSDictionary *)headerFields forURL:(NSURL *)theURL

    Parameters

    headerFields

    The header fields used to create the NSHTTPCookie objects.

    theURL

    The URL associated with the created cookies.

    Return Value

    The array of created cookies.

    Discussion

    This method ignores irrelevant header fields in headerFields, allowing dictionaries to contain additional data.

    If headerFields does not specify a domain for a given cookie, the cookie is created with a default domain value of theURL.

    If headerFields does not specify a path for a given cookie, the cookie is created with a default path value of "/".

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • Creates and initializes an NSHTTPCookie object using the provided properties.

    Declaration

    Objective-C

    + (NSHTTPCookie *)cookieWithProperties:(NSDictionary *)properties

    Parameters

    properties

    The properties for the new cookie object, expressed as key value pairs.

    Return Value

    The newly created cookie object. Returns nil if the provided properties are invalid.

    Discussion

    To successfully create a cookie, you must provide values for (at least) the NSHTTPCookiePath, NSHTTPCookieName, and NSHTTPCookieValue keys, and either the NSHTTPCookieOriginURL key or the NSHTTPCookieDomain key.

    See Constants for more information on the available cookie attribute constants and the constraints imposed on the values in the dictionary.

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in iOS 2.0 and later

  • Returns an initialized NSHTTPCookie object using the provided properties.

    Declaration

    Swift

    init?(properties properties: [NSObject : AnyObject])

    Objective-C

    - (instancetype)initWithProperties:(NSDictionary *)properties

    Parameters

    properties

    The properties for the new cookie object, expressed as key value pairs.

    Return Value

    The initialized cookie object. Returns nil if the provided properties are invalid.

    Discussion

    To successfully create a cookie, you must provide values for (at least) the NSHTTPCookiePath, NSHTTPCookieName, and NSHTTPCookieValue keys, and either the NSHTTPCookieOriginURL key or the NSHTTPCookieDomain key.

    See Constants for more information on the available cookie attribute constants and the constraints imposed on the values in the dictionary.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • Returns a dictionary of header fields corresponding to a provided array of cookies.

    Declaration

    Swift

    class func requestHeaderFieldsWithCookies(_ cookies: [AnyObject]) -> [NSObject : AnyObject]

    Objective-C

    + (NSDictionary *)requestHeaderFieldsWithCookies:(NSArray *)cookies

    Parameters

    cookies

    The cookies from which the header fields are created.

    Return Value

    The dictionary of header fields created from the provided cookies.

    Discussion

    To send these headers as part of a URL request to a remote server, create an NSMutableURLRequest object, then call the setAllHTTPHeaderFields: or setValue:forHTTPHeaderField: method to set the provided headers for the request. Finally, initialize and start an NSURLSessionTask, NSURLConnection, or NSURLDownload object based on that request object.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • comment comment Property

    The receiver's comment string. (read-only)

    Declaration

    Swift

    var comment: String? { get }

    Objective-C

    @property(readonly, copy) NSString *comment

    Discussion

    The receiver’s comment string or nil if the cookie has no comment. This string is suitable for presentation to the user, explaining the contents and purpose of this cookie.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • The receiver’s comment URL. (read-only)

    Declaration

    Swift

    @NSCopying var commentURL: NSURL? { get }

    Objective-C

    @property(readonly, copy) NSURL *commentURL

    Discussion

    The receiver’s comment URL or nil if the cookie has none. This value specifies a URL which is suitable for presentation to the user as a link for further information about this cookie.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • domain domain Property

    The domain of the receiver’s cookie. (read-only)

    Declaration

    Swift

    var domain: String { get }

    Objective-C

    @property(readonly, copy) NSString *domain

    Discussion

    If the domain does not start with a dot, then the cookie is only sent to the exact host specified by the domain. If the domain does start with a dot, then the cookie is sent to other hosts in that domain as well, subject to certain restrictions. See RFC 2965 for more detail.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • The receiver’s expiration date. (read-only)

    Declaration

    Swift

    @NSCopying var expiresDate: NSDate! { get }

    Objective-C

    @property(readonly, copy) NSDate *expiresDate

    Discussion

    The receiver’s expiration date, or nil if there is no specific expiration date such as in the case of “session-only” cookies. The expiration date is the date when the cookie should be deleted.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • HTTPOnly HTTPOnly Property

    A boolean value that indicates whether the receiver should only be sent to HTTP servers per RFC 2965. (read-only)

    Declaration

    Swift

    var HTTPOnly: Bool { get }

    Objective-C

    @property(readonly, getter=isHTTPOnly) BOOL HTTPOnly

    Discussion

    Returns YEStrue if this cookie should only be sent via HTTP headers, NOfalse otherwise.

    Cookies may be marked as HTTP only by a server (or by a javascript). Cookies marked as such must only be sent via HTTP Headers in HTTP requests for URL's that match both the path and domain of the respective cookies.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 8.0 and later

  • secure secure Property

    A boolean value that indicates whether this cookie should only be sent over secure channels. (read-only)

    Declaration

    Swift

    var secure: Bool { get }

    Objective-C

    @property(readonly, getter=isSecure) BOOL secure

    Discussion

    YEStrue if this cookie should only be sent over secure channels, otherwise NOfalse.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 8.0 and later

  • A boolean value that indicates whether the receiver should be discarded at the end of the session (regardless of expiration date). (read-only)

    Declaration

    Swift

    var sessionOnly: Bool { get }

    Objective-C

    @property(readonly, getter=isSessionOnly) BOOL sessionOnly

    Discussion

    YEStrue if the receiver should be discarded at the end of the session (regardless of expiration date), otherwise NOfalse.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 8.0 and later

  • name name Property

    The receiver’s name. (read-only)

    Declaration

    Swift

    var name: String { get }

    Objective-C

    @property(readonly, copy) NSString *name

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • path path Property

    The receiver’s path. (read-only)

    Declaration

    Swift

    var path: String? { get }

    Objective-C

    @property(readonly, copy) NSString *path

    Discussion

    The cookie will be sent with requests for this path in the cookie's domain, and all paths that have this prefix. A path of "/" means the cookie will be sent for all URLs in the domain.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • portList portList Property

    The receiver's port list. (read-only)

    Declaration

    Swift

    var portList: [AnyObject]? { get }

    Objective-C

    @property(readonly, copy) NSArray *portList

    Discussion

    The list of ports for the cookie, returned as an array of NSNumber objects containing integers. If the cookie has no port list this method returns nil and the cookie will be sent to any port. Otherwise, the cookie is only sent to ports specified in the port list.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • The receiver’s cookie properties. (read-only)

    Declaration

    Swift

    var properties: [NSObject : AnyObject]? { get }

    Objective-C

    @property(readonly, copy) NSDictionary *properties

    Discussion

    This dictionary can be used with initWithProperties: or cookieWithProperties: to create an equivalent NSHTTPCookie object.

    See initWithProperties: for more information on the constraints imposed on the properties dictionary.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • value value Property

    The receiver’s value. (read-only)

    Declaration

    Swift

    var value: String? { get }

    Objective-C

    @property(readonly, copy) NSString *value

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • version version Property

    The receiver’s version. (read-only)

    Declaration

    Swift

    var version: Int { get }

    Objective-C

    @property(readonly) NSUInteger version

    Discussion

    The receiver's version. Version 0 maps to “old-style” Netscape cookies. Version 1 maps to RFC 2965 cookies.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • These constants define the supported keys in a cookie attributes dictionary.

    Declaration

    Swift

    let NSHTTPCookieComment: String let NSHTTPCookieCommentURL: String let NSHTTPCookieDiscard: String let NSHTTPCookieDomain: String let NSHTTPCookieExpires: String let NSHTTPCookieMaximumAge: String let NSHTTPCookieName: String let NSHTTPCookieOriginURL: String let NSHTTPCookiePath: String let NSHTTPCookiePort: String let NSHTTPCookieSecure: String let NSHTTPCookieValue: String let NSHTTPCookieVersion: String

    Objective-C

    NSString *NSHTTPCookieComment; NSString *NSHTTPCookieCommentURL; NSString *NSHTTPCookieDiscard; NSString *NSHTTPCookieDomain; NSString *NSHTTPCookieExpires; NSString *NSHTTPCookieMaximumAge; NSString *NSHTTPCookieName; NSString *NSHTTPCookieOriginURL; NSString *NSHTTPCookiePath; NSString *NSHTTPCookiePort; NSString *NSHTTPCookieSecure; NSString *NSHTTPCookieValue; NSString *NSHTTPCookieVersion;

    Constants

    • NSHTTPCookieComment

      NSHTTPCookieComment

      An NSString object containing the comment for the cookie.

      Only valid for Version 1 cookies and later. This cookie attribute is optional.

      Available in iOS 2.0 and later

    • NSHTTPCookieCommentURL

      NSHTTPCookieCommentURL

      An NSURL object or NSString object containing the comment URL for the cookie.

      Only valid for Version 1 cookies or later. This cookie attribute is optional.

      Available in iOS 2.0 and later

    • NSHTTPCookieDiscard

      NSHTTPCookieDiscard

      An NSString object stating whether the cookie should be discarded at the end of the session.

      String value must be either "TRUE" or "FALSE". This cookie attribute is optional. The default is "FALSE", unless this cookie is version 1 or greater and a value for NSHTTPCookieMaximumAge is not specified, in which case it is assumed to be "TRUE".

      Available in iOS 2.0 and later

    • NSHTTPCookieDomain

      NSHTTPCookieDomain

      An NSString object containing the domain for the cookie.

      If this cookie attribute is missing, the domain is inferred from the value for NSHTTPCookieOriginURL. If you do not specify a value for NSHTTPCookieOriginURL, you must specify a value for NSHTTPCookieDomain.

      Available in iOS 2.0 and later

    • NSHTTPCookieExpires

      NSHTTPCookieExpires

      An NSDate object or NSString object specifying the expiration date for the cookie.

      This cookie attribute is only used for Version 0 cookies. This cookie attribute is optional.

      Available in iOS 2.0 and later

    • NSHTTPCookieMaximumAge

      NSHTTPCookieMaximumAge

      An NSString object containing an integer value stating how long in seconds the cookie should be kept, at most.

      Only valid for Version 1 cookies and later. Default is "0". This cookie attribute is optional.

      Available in iOS 2.0 and later

    • NSHTTPCookieName

      NSHTTPCookieName

      An NSString object containing the name of the cookie. This cookie attribute is required.

      Available in iOS 2.0 and later

    • NSHTTPCookieOriginURL

      NSHTTPCookieOriginURL

      An NSURL or NSString object containing the URL that set this cookie.

      If you do not provide a value for NSHTTPCookieOriginURL, you must provide a value for NSHTTPCookieDomain.

      Available in iOS 2.0 and later

    • NSHTTPCookiePath

      NSHTTPCookiePath

      An NSString object containing the path for the cookie.

      This cookie attribute is required.

      Available in iOS 2.0 and later

    • NSHTTPCookiePort

      NSHTTPCookiePort

      An NSString object containing comma-separated integer values specifying the ports for the cookie.

      Only valid for Version 1 cookies or later. The default value is an empty string (""). This cookie attribute is optional.

      Available in iOS 2.0 and later

    • NSHTTPCookieSecure

      NSHTTPCookieSecure

      An NSString object indicating that the cookie should be transmitted only over secure channels.

      Providing any value for this key indicates that the cookie should remain secure.

      Available in iOS 2.0 and later

    • NSHTTPCookieValue

      NSHTTPCookieValue

      An NSString object containing the value of the cookie.

      This cookie attribute is required.

      Available in iOS 2.0 and later

    • NSHTTPCookieVersion

      NSHTTPCookieVersion

      An NSString object that specifies the version of the cookie.

      Must be either "0" or "1". The default is "0". This cookie attribute is optional.

      Available in iOS 2.0 and later