Classes
The following classes are available globally.
-
AFSecurityPolicy
evaluates server trust against pinned X.509 certificates and public keys over secure connections.Adding pinned SSL certificates to your app helps prevent man-in-the-middle attacks and other vulnerabilities. Applications dealing with sensitive customer data or financial information are strongly encouraged to route all communication over an HTTPS connection with SSL pinning configured and enabled.
See moreDeclaration
Objective-C
@interface AFSecurityPolicy : NSObject <NSSecureCoding, NSCopying>
Swift
class AFSecurityPolicy : NSObject, NSSecureCoding, NSCopying
-
AFNetworkReachabilityManager
monitors the reachability of domains, and addresses for both WWAN and WiFi network interfaces.Reachability can be used to determine background information about why a network operation failed, or to trigger a network operation retrying when a connection is established. It should not be used to prevent a user from initiating a network request, as it’s possible that an initial request may be required to establish reachability.
See Apple’s Reachability Sample Code ( https://developer.apple.com/library/ios/samplecode/reachability/ )
Warning
Instances ofAFNetworkReachabilityManager
must be started with-startMonitoring
before reachability status can be determined.Declaration
Objective-C
@interface AFNetworkReachabilityManager : NSObject
Swift
class AFNetworkReachabilityManager : NSObject
-
AFHTTPRequestSerializer
conforms to theAFURLRequestSerialization
&AFURLResponseSerialization
protocols, offering a concrete base implementation of query string / URL form-encoded parameter serialization and default request headers, as well as response status code and content type validation.Any request or response serializer dealing with HTTP is encouraged to subclass
See moreAFHTTPRequestSerializer
in order to ensure consistent default behavior.Declaration
Objective-C
@interface AFHTTPRequestSerializer : NSObject <AFURLRequestSerialization>
Swift
class AFHTTPRequestSerializer : NSObject, AFURLRequestSerialization
-
See moreAFJSONRequestSerializer
is a subclass ofAFHTTPRequestSerializer
that encodes parameters as JSON usingNSJSONSerialization
, setting theContent-Type
of the encoded request toapplication/json
.Declaration
Objective-C
@interface AFJSONRequestSerializer : AFHTTPRequestSerializer
Swift
class AFJSONRequestSerializer : AFHTTPRequestSerializer
-
See moreAFPropertyListRequestSerializer
is a subclass ofAFHTTPRequestSerializer
that encodes parameters as JSON usingNSPropertyListSerializer
, setting theContent-Type
of the encoded request toapplication/x-plist
.Declaration
Objective-C
@interface AFPropertyListRequestSerializer : AFHTTPRequestSerializer
Swift
class AFPropertyListRequestSerializer : AFHTTPRequestSerializer
-
AFHTTPResponseSerializer
conforms to theAFURLRequestSerialization
&AFURLResponseSerialization
protocols, offering a concrete base implementation of query string / URL form-encoded parameter serialization and default request headers, as well as response status code and content type validation.Any request or response serializer dealing with HTTP is encouraged to subclass
See moreAFHTTPResponseSerializer
in order to ensure consistent default behavior.Declaration
Objective-C
@interface AFHTTPResponseSerializer : NSObject <AFURLResponseSerialization>
Swift
class AFHTTPResponseSerializer : NSObject, AFURLResponseSerialization
-
AFJSONResponseSerializer
is a subclass ofAFHTTPResponseSerializer
that validates and decodes JSON responses.By default,
AFJSONResponseSerializer
accepts the following MIME types, which includes the official standard,application/json
, as well as other commonly-used types:application/json
text/json
text/javascript
In RFC 7159 - Section 8.1, it states that JSON text is required to be encoded in UTF-8, UTF-16, or UTF-32, and the default encoding is UTF-8. NSJSONSerialization provides support for all the encodings listed in the specification, and recommends UTF-8 for efficiency. Using an unsupported encoding will result in serialization error. See the
See moreNSJSONSerialization
documentation for more details.Declaration
Objective-C
@interface AFJSONResponseSerializer : AFHTTPResponseSerializer
Swift
class AFJSONResponseSerializer : AFHTTPResponseSerializer
-
AFXMLParserResponseSerializer
is a subclass ofAFHTTPResponseSerializer
that validates and decodes XML responses as anNSXMLParser
objects.By default,
AFXMLParserResponseSerializer
accepts the following MIME types, which includes the official standard,application/xml
, as well as other commonly-used types:application/xml
text/xml
Declaration
Objective-C
@interface AFXMLParserResponseSerializer : AFHTTPResponseSerializer
Swift
class AFXMLParserResponseSerializer : AFHTTPResponseSerializer
-
AFPropertyListResponseSerializer
is a subclass ofAFHTTPResponseSerializer
that validates and decodes XML responses as anNSXMLDocument
objects.By default,
AFPropertyListResponseSerializer
accepts the following MIME types:application/x-plist
Declaration
Objective-C
@interface AFPropertyListResponseSerializer : AFHTTPResponseSerializer
Swift
class AFPropertyListResponseSerializer : AFHTTPResponseSerializer
-
AFImageResponseSerializer
is a subclass ofAFHTTPResponseSerializer
that validates and decodes image responses.By default,
AFImageResponseSerializer
accepts the following MIME types, which correspond to the image formats supported by UIImage or NSImage:image/tiff
image/jpeg
image/gif
image/png
image/ico
image/x-icon
image/bmp
image/x-bmp
image/x-xbitmap
image/x-win-bitmap
Declaration
Objective-C
@interface AFImageResponseSerializer : AFHTTPResponseSerializer
Swift
class AFImageResponseSerializer : AFHTTPResponseSerializer
-
See moreAFCompoundSerializer
is a subclass ofAFHTTPResponseSerializer
that delegates the response serialization to the firstAFHTTPResponseSerializer
object that returns an object forresponseObjectForResponse:data:error:
, falling back on the default behavior ofAFHTTPResponseSerializer
. This is useful for supporting multiple potential types and structures of server responses with a single serializer.Declaration
Objective-C
@interface AFCompoundResponseSerializer : AFHTTPResponseSerializer
Swift
class AFCompoundResponseSerializer : AFHTTPResponseSerializer
-
AFURLSessionManager
creates and manages anNSURLSession
object based on a specifiedNSURLSessionConfiguration
object, which conforms to<NSURLSessionTaskDelegate>
,<NSURLSessionDataDelegate>
,<NSURLSessionDownloadDelegate>
, and<NSURLSessionDelegate>
.Subclassing Notes
This is the base class for
AFHTTPSessionManager
, which adds functionality specific to making HTTP requests. If you are looking to extendAFURLSessionManager
specifically for HTTP, consider subclassingAFHTTPSessionManager
instead.NSURLSession & NSURLSessionTask Delegate Methods
AFURLSessionManager
implements the following delegate methods:NSURLSessionDelegate
URLSession:didBecomeInvalidWithError:
URLSession:didReceiveChallenge:completionHandler:
URLSessionDidFinishEventsForBackgroundURLSession:
NSURLSessionTaskDelegate
URLSession:willPerformHTTPRedirection:newRequest:completionHandler:
URLSession:task:didReceiveChallenge:completionHandler:
URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:
URLSession:task:needNewBodyStream:
URLSession:task:didCompleteWithError:
NSURLSessionDataDelegate
URLSession:dataTask:didReceiveResponse:completionHandler:
URLSession:dataTask:didBecomeDownloadTask:
URLSession:dataTask:didReceiveData:
URLSession:dataTask:willCacheResponse:completionHandler:
NSURLSessionDownloadDelegate
URLSession:downloadTask:didFinishDownloadingToURL:
URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:
URLSession:downloadTask:didResumeAtOffset:expectedTotalBytes:
If any of these methods are overridden in a subclass, they must call the
super
implementation first.Network Reachability Monitoring
Network reachability status and change monitoring is available through the
reachabilityManager
property. Applications may choose to monitor network reachability conditions in order to prevent or suspend any outbound requests. SeeAFNetworkReachabilityManager
for more details.NSCoding Caveats
- Encoded managers do not include any block properties. Be sure to set delegate callback blocks when using
-initWithCoder:
orNSKeyedUnarchiver
.
NSCopying Caveats
-copy
and-copyWithZone:
return a new manager with a newNSURLSession
created from the configuration of the original.Operation copies do not include any delegate callback blocks, as they often strongly captures a reference to
self
, which would otherwise have the unintuitive side-effect of pointing to the original session manager when copied.
Warning
Managers for background sessions must be owned for the duration of their use. This can be accomplished by creating an application-wide or shared singleton instance.
Declaration
Objective-C
@interface AFURLSessionManager : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, NSSecureCoding, NSCopying>
Swift
class AFURLSessionManager : NSObject, URLSessionDelegate, URLSessionTaskDelegate, URLSessionDataDelegate, URLSessionDownloadDelegate, NSSecureCoding, NSCopying
-
AFHTTPSessionManager
is a subclass ofAFURLSessionManager
with convenience methods for making HTTP requests. When abaseURL
is provided, requests made with theGET
/POST
/ et al. convenience methods can be made with relative paths.Subclassing Notes
Developers targeting iOS 7 or Mac OS X 10.9 or later that deal extensively with a web service are encouraged to subclass
AFHTTPSessionManager
, providing a class method that returns a shared singleton object on which authentication and other configuration can be shared across the application.Methods to Override
To change the behavior of all data task operation construction, which is also used in the
GET
/POST
/ et al. convenience methods, overridedataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
.Serialization
Requests created by an HTTP client will contain default headers and encode parameters according to the
requestSerializer
property, which is an object conforming to<AFURLRequestSerialization>
.Responses received from the server are automatically validated and serialized by the
responseSerializers
property, which is an object conforming to<AFURLResponseSerialization>
URL Construction Using Relative Paths
For HTTP convenience methods, the request serializer constructs URLs from the path relative to the
-baseURL
, usingNSURL +URLWithString:relativeToURL:
, when provided. IfbaseURL
isnil
,path
needs to resolve to a validNSURL
object usingNSURL +URLWithString:
.Below are a few examples of how
baseURL
and relative paths interact:NSURL *baseURL = [NSURL URLWithString:@“http://example.com/v1/”]; [NSURL URLWithString:@“foo” relativeToURL:baseURL]; // http://example.com/v1/foo [NSURL URLWithString:@“foo?bar=baz” relativeToURL:baseURL]; // http://example.com/v1/foo?bar=baz [NSURL URLWithString:@“/foo” relativeToURL:baseURL]; // http://example.com/foo [NSURL URLWithString:@“foo/” relativeToURL:baseURL]; // http://example.com/v1/foo [NSURL URLWithString:@“/foo/” relativeToURL:baseURL]; // http://example.com/foo/ [NSURL URLWithString:@“http://example2.com/” relativeToURL:baseURL]; // http://example2.com/
Also important to note is that a trailing slash will be added to any
baseURL
without one. This would otherwise cause unexpected behavior when constructing URLs using paths without a leading slash.Warning
Managers for background sessions must be owned for the duration of their use. This can be accomplished by creating an application-wide or shared singleton instance.Declaration
Objective-C
@interface AFHTTPSessionManager : AFURLSessionManager <NSSecureCoding, NSCopying>
Swift
class AFHTTPSessionManager : AFURLSessionManager, NSSecureCoding, NSCopying