iOS Developer Library

Developer

Foundation Framework Reference NSURLResponse Class Reference

Options
Deployment Target:

On This Page
Language:

NSURLResponse

Inherits From


Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 2.0 and later

The NSURLResponse class encapsulates the metadata associated with the response to a a URL load request in a manner independent of protocol and URL scheme.

The related NSHTTPURLResponse class is a commonly used subclass of NSURLResponse whose objects represent a response to an HTTP URL load request and store additional protocol-specific information such as the response headers. Whenever you make an HTTP request, the NSURLResponse object you get back is actually an instance of the NSHTTPURLResponse class.

  • Returns an initialized NSURLResponse object with the URL, MIME type, length, and text encoding set to given values.

    Declaration

    Swift

    init(URL URL: NSURL, MIMEType MIMEType: String?, expectedContentLength length: Int, textEncodingName name: String?)

    Objective-C

    - (instancetype)initWithURL:(NSURL *)URL MIMEType:(NSString *)MIMEType expectedContentLength:(NSInteger)length textEncodingName:(NSString *)name

    Parameters

    URL

    The URL for the new object.

    MIMEType

    The MIME type.

    length

    The expected content length.This value should be –1 if the expected length is undetermined

    name

    The text encoding name. This value may be nil.

    Return Value

    An initialized NSURLResponse object with the URL set to URL, the MIME type set to MIMEType, length set to length, and text encoding name set to name.

    Discussion

    This is the designated initializer for NSURLResponse.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • The expected length of the response’s content. (read-only)

    Declaration

    Swift

    var expectedContentLength: Int64 { get }

    Objective-C

    @property(readonly) long long expectedContentLength

    Discussion

    This property’s value is NSURLResponseUnknownLength if the length can’t be determined.

    Some protocol implementations report the content length as part of the response, but not all protocols guarantee to deliver that amount of data. Clients should be prepared to deal with more or less data.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • A suggested filename for the response data. (read-only)

    Declaration

    Swift

    var suggestedFilename: String? { get }

    Objective-C

    @property(readonly, copy) NSString *suggestedFilename

    Discussion

    Accessing this property attempts to generate a filename using the following information, in order:

    1. A filename specified using the content disposition header.

    2. The last path component of the URL.

    3. The host of the URL.

    If the host of URL can't be converted to a valid filename, the filename “unknown” is used.

    In most cases, this property appends the proper file extension based on the MIME type. Accessing this property always returns a valid filename regardless of whether the resource is saved to disk.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • MIMEType MIMEType Property

    The MIME type of the response. (read-only)

    Declaration

    Swift

    var MIMEType: String? { get }

    Objective-C

    @property(readonly, copy) NSString *MIMEType

    Discussion

    The MIME type is often provided by the response’s originating source. However, that value may be changed or corrected by a protocol implementation if it can be determined that the response’s source reported the information incorrectly.

    If the response’s originating source does not provide a MIME type, an attempt to guess the MIME type may be made.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • The name of the text encoding provided by the response’s originating source. (read-only)

    Declaration

    Swift

    var textEncodingName: String? { get }

    Objective-C

    @property(readonly, copy) NSString *textEncodingName

    Discussion

    If no text encoding was provided by the protocol, this property’s value is nil.

    You can convert this string to a CFStringEncoding value by calling CFStringConvertIANACharSetNameToEncoding. You can subsequently convert that value to an NSStringEncoding value by calling CFStringConvertEncodingToNSStringEncoding.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • URL URL Property

    The URL for the response. (read-only)

    Declaration

    Swift

    @NSCopying var URL: NSURL? { get }

    Objective-C

    @property(readonly, copy) NSURL *URL

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later

  • The following error code is returned by expectedContentLength.

    Declaration

    Objective-C

    #define NSURLResponseUnknownLength ((long long)-1)

    Constants

    • NSURLResponseUnknownLength

      NSURLResponseUnknownLength

      Returned when the response length cannot be determined in advance of receiving the data from the server. For example, NSURLResponseUnknownLength is returned when the server HTTP response does not include a Content-Length header.

      Available in iOS 2.0 and later