Afnetworking Study Notes

Source: Internet
Author: User

Starting from 3.0

In a blink of an eye, AF has been updated to version 3.0. Currently the latest version on Cocoapods is 3.0 beta1. In the 3.0 version, AF is completely used NSURLSession instead NSURLConnection . It took some time to learn NSURLSession , and learning here finally came in handy. Here is the main study of the use of version 3.0. Hopefully, the project will be able to go over to afnetwoking version 3.0 smoothly. In addition, with objective-c slowly being replaced by Swift, afnetworking 3.0 is probably the last big version of the update. This article will continue to update with an update, but also a continuous learning process.


In the 3.0 era, AFN streamlined the structure and was fully used NSURLSession .

There are only a few managers left in Beta1.

AFHTTPSessionManageris AFURLSessionManager the child class.


AFURLSessionManagerThe following NSURLSession proxy methods are implemented

    • URLSession:didBecomeInvalidWithError:
    • URLSession:didReceiveChallenge:completionHandler:
    • URLSessionDidFinishEventsForBackgroundURLSession:
    • URLSession:willPerformHTTPRedirection:newRequest:completionHandler:
    • URLSession:task:didReceiveChallenge:completionHandler:
    • URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:
    • URLSession:task:didCompleteWithError:
    • URLSession:dataTask:didReceiveResponse:completionHandler:
    • URLSession:dataTask:didBecomeDownloadTask:
    • URLSession:dataTask:didReceiveData:
    • URLSession:dataTask:willCacheResponse:completionHandler:
    • URLSession:downloadTask:didFinishDownloadingToURL:
    • URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesWritten:totalBytesExpectedToWrite:
    • URLSession:downloadTask:didResumeAtOffset:expectedTotalBytes:
Member properties

In AFURLSessionManager , the main three properties are as follows

sessionA session is implemented and operationQueue is an operation queue. responseSerializeris an object that implements the AFURLResponseSerialization protocol.

The manager also includes objects for security protocols and connectivity objects. These two classes will be discussed later.

The following is the contents of the task, including the current Session

Callback block queue, including two sections in the primary and private queues


Initialize method

Create a NSURLSessionDataTask data-sex task

Create an `NSURLSessionUploadTask upload task

Create a NSURLSessionDownloadTask download task

To get a specific taskprogress进度

Session Delegate Callbacks 设置会话代理回调

Task Delegate Callbacks 设置任务代理回调

When the task requires a new request body to send to the server.

This block is set when the HTTP request callback has a redirect

Set this challenge when a request requires special authentication

Set up a block to track upload progress

Set a block to execute when the task is completed

Setting Data Task Delegate Callbacks 设置数据任务代理的回调

Set a callback block when the data task gets response

Set a block to execute when the data task becomes a download task

Set up a block when the data task gets to the data

Set a block to absolutely cache data tasks

Download Task Delegate Callbacks 下载任务代理回调

Set the block to download after the task is completed

Set up block to track the progress of the download task

Set block to execute when loading task execution/recovery

The content of the header file is basically the above. You can see the whole AF system is very clear and complete, there is no superfluous things, the head file only exposes what should be exposed, it is worth learning.

Use the example used AFURLSessionManager

From the source code can be seen, the AFURLSessionManager realization of

NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, NSSecureCoding, NSCopying

First you need to set the URL and nsurlconfirguration

This is the public API using the Baidu API store

Then initialize the manager

Set Responseserializer

Initialize Request

Setting the request

Generate the corresponding according to request NSURLSessionTask .

Perform tasks

Take a look at the post-execution information.

This is because of the use of the AFHTTPResponseSerializer (API problem, only support Text/plain) so after getting the data, we ourselves want JSON serialization. If it is a well-designed API, it AFJSONRequestSerializer can be used directly in the callback to get the data in JSON format.

As you can see, the whole use is still very convenient. We can configure different settings according to our own needs.

Afnetworking Study Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.