iOS Developer Library

Developer

Foundation Framework Reference NSHTTPURLResponse Class Reference

Options
Deployment Target:

On This Page
Language:

NSHTTPURLResponse

The NSHTTPURLResponse class is a subclass of NSURLResponse that provides methods for accessing information specific to HTTP protocol responses. Whenever you make HTTP URL load requests, any response objects you get back from the NSURLSession, NSURLConnection, or NSURLDownload class are instances of the NSHTTPURLResponse class.

Inheritance


Import Statement


import Foundation @import Foundation;

Availability


Available in iOS 2.0 and later.
  • Initializes an HTTP URL response object with a status code, protocol version, and response headers.

    Declaration

    Swift

    init?(URL url: NSURL, statusCode statusCode: Int, HTTPVersion HTTPVersion: String?, headerFields headerFields: [NSObject : AnyObject]?)

    Objective-C

    - (instancetype)initWithURL:(NSURL *)url statusCode:(NSInteger)statusCode HTTPVersion:(NSString *)HTTPVersion headerFields:(NSDictionary *)headerFields

    Parameters

    url

    The URL from which the response was generated.

    statusCode

    The HTTP status code to return (404, for example). See RFC 2616 for details.

    HTTPVersion

    The version of the HTTP response as returned by the server. This is typically represented as "HTTP/1.1".

    headerFields

    A dictionary representing the keys and values from the server’s response header.

    Return Value

    An initialized NSHTTPURLResponse object or nil if an error occurred during initialization.

    Import Statement

    import Foundation

    Availability

    Available in iOS 5.0 and later.

  • All HTTP header fields of the receiver. (read-only)

    Declaration

    Swift

    var allHeaderFields: [NSObject : AnyObject] { get }

    Objective-C

    @property(readonly, copy) NSDictionary *allHeaderFields

    Discussion

    A dictionary containing all the HTTP header fields received as part of the server’s response. By examining this dictionary clients can see the “raw” header information returned by the HTTP server.

    The keys in this dictionary are the header field names, as received from the server. See RFC 2616 for a list of commonly used HTTP header fields.

    HTTP headers are case insensitive. To simplify your code, certain header field names are canonicalized into their standard form. For example, if the server sends a content-length header, it is automatically adjusted to be Content-Length.

    The returned dictionary of headers is configured to be case-preserving during the set operation (unless the key already exists with a different case), and case-insensitive when looking up keys.

    For example, if you set the header X-foo, and then later set the header X-Foo, the dictionary’s key will be X-foo, but the value will taken from the X-Foo header.

    Special Considerations

    Prior to OS X v10.7 and iOS 5, canonicalization occurred for all header fields. The case-preserving dictionary was first introduced in OS X v10.7.2 and iOS 5.

    Import Statement

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Returns a localized string corresponding to a specified HTTP status code.

    Declaration

    Swift

    class func localizedStringForStatusCode(_ statusCode: Int) -> String

    Objective-C

    + (NSString *)localizedStringForStatusCode:(NSInteger)statusCode

    Parameters

    statusCode

    The HTTP status code. See RFC 2616 for details.

    Return Value

    A localized string suitable for displaying to users that describes the specified status code.

    Import Statement

    import Foundation

    Availability

    Available in iOS 2.0 and later.

    See Also

    – statusCode

  • The receiver’s HTTP status code. (read-only)

    Declaration

    Swift

    var statusCode: Int { get }

    Objective-C

    @property(readonly) NSInteger statusCode

    Discussion

    See RFC 2616 for details.

    Import Statement

    import Foundation

    Availability

    Available in iOS 2.0 and later.