NSURLResponse Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in iOS 2.0 and later.
Companion guide
Declared in
NSURLResponse.h
Related sample code

Overview

NSURLResponse declares the programmatic interface for an object that accesses the response returned by an NSURLRequest instance.

NSURLResponse encapsulates the metadata associated with a URL load in a manner independent of protocol and URL scheme.

NSHTTPURLResponse is a subclass of NSURLResponse that provides methods for accessing information specific to HTTP protocol responses. An NSHTTPURLResponse object represents a response to an HTTP URL load request.

Adopted Protocols

NSCoding
NSCopying

Tasks

Creating a Response

Getting the Response Properties

Instance Methods

expectedContentLength

Returns the receiver’s expected content length

- (long long)expectedContentLength
Return Value

The receiver’s expected content length, or NSURLResponseUnknownLength if the length can’t be determined.

Discussion

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.

Availability
  • Available in iOS 2.0 and later.
Related Sample Code
Declared In
NSURLResponse.h

initWithURL:MIMEType:expectedContentLength:textEncodingName:

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

- (id)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.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSURLResponse.h

initWithURL:statusCode:HTTPVersion:headerFields:

Returns an initialized response with the specified values.

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

The URL for the new object.

statusCode

The HTTP status code to return.

HTTPVersion

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

headerFields

A dictionary representing the header keys and values of the server response.

Return Value

An initialized NSURLResponse object or NULL if an error occurred during initialization.

Availability
  • Available in iOS v5.0 and later.

MIMEType

Returns the receiver’s MIME type.

- (NSString *)MIMEType
Return Value

The receiver’s MIME type.

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.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSURLResponse.h

suggestedFilename

Returns a suggested filename for the response data.

- (NSString *)suggestedFilename
Return Value

A suggested filename for the response data.

Discussion

The method tries to create a filename using the following, 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 method appends the proper file extension based on the MIME type. This method will always return a valid filename regardless of whether or not the resource is saved to disk.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSURLResponse.h

textEncodingName

Returns the name of the receiver’s text encoding provided by the response’s originating source.

- (NSString *)textEncodingName
Return Value

The name of the receiver’s text encoding provided by the response’s originating source, or nil if no text encoding was provided by the protocol

Discussion

Clients can convert this string to an NSStringEncoding or a CFStringEncoding using the methods and functions available in the appropriate framework.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSURLResponse.h

URL

Returns the receiver’s URL.

- (NSURL *)URL
Return Value

The receiver’s URL.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSURLResponse.h

Constants

Response Length Unknown Error

The following error code is returned by expectedContentLength.

#define NSURLResponseUnknownLength ((long long)-1)
Constants
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.

Declared in NSURLResponse.h.


Did this document help you? Yes It's good, but... Not helpful...