Afnetworking migration of New version 3.0

Source: Internet
Author: User
Tags deprecated

Afnetworking
For this open source Library, I believe that no iOS developers will be unfamiliar, this powerful and perfect network library brings us too much convenience, in order to cater to the new version of iOS upgrade, afnetworking also updated 3.0.0-beta.1, but the use of people may not be many, but we will be used in the future , so learn it first.

Afnetworking has removed all support based on the Nsurlconnection API in version 3.0. If the project has previously used these APIs, then we need to upgrade to the afnetworking version of the Nsurlsession-based API.

First of all, we don't have to worry about what the new version actually changed, so we'll get to know what the afnetworking really did for us, and let us save a lot of effort to deal with the Web download.

Here we just need to understand briefly, because this library is very large, use a lot of knowledge of the bottom, if the realization of the principle of interest, you can look at Bang's blog.

Implementation process of GET request
//使用代码AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];    manager.responseSerializer = [AFHTTPResponseSerializer serializer];    [manager GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) { //成功 NSDictionary *obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; } failure:^(NSURLSessionDataTask *task, NSError *error) { //失败 }];

Flow chart
    1. Before we call the Get function, we need to create a afhttpsessionmanager, which we used before is the general Afhttprequestoperationmanager.
    2. After calling the GET request, we generally do not need to do, because those cumbersome and complex network communication to him to do, he will first call Afnetworkreachabilitymanager to verify whether the network.
    3. The parameter is then serialized for network requests.
    4. Call the Afsecuritypolicy class, afsecuritypolicy the certificate used to validate the HTTPS request, and simply establish the connection. Specific steps to establish a reference blog.
    5. The connection is established, the certificate is validated successfully, and the request server begins.
    6. Returns data from the server, parses the server data, and deserializes the data.
    7. We get the data type, which can be parsed directly into a dictionary using JSON parsing.

In this realization process He also helped us to do a thread, asynchronous download, check whether the data is legitimate, image decompression and other cumbersome things, so we can use so comfortable, but we still need to study the internal implementation, only to maintain a knowledge of the heart, we can study deeper, get more.

We already know the implementation process, so let's take a look at the new version. afnetworkingWhat changes have been made to 1. The Nsurlconnection API is deprecated

Afnetworking 1.0 builds on Nsurlconnection's underlying API, Afnetworking 2.0 starts using Nsurlconnection's underlying API, and options for newer Nsurlsession-based APIs. Afnetworking 3.0 is now fully based on the Nsurlsession API, which reduces the burden of maintenance. In Xcode 7, Nsurlconnection's API has been officially deprecated by Apple. While the API will continue to run, there will be no new features to be added, and Apple has notified all network-based features to fully enable nsurlsession to evolve.
Afnetworking 2.X will continue to get critical pitfalls and security patches, but no new features will be added.
Deprecated classes
The following classes have been deprecated from Afnetworking 3.0:

    • Afurlconnectionoperation
    • Afhttprequestoperation
    • Afhttprequestoperationmanager
2. Modified Classes

The following class contains an internal implementation of the Nsurlconnection-based API. They have been used nsurlsession refactoring:

    • Uiimageview+afnetworking
    • Uiwebview+afnetworking
    • Uibutton+afnetworking
3. Migration

Afhttprequestoperationmanager Core Code
If you have used Afhttprequestoperationmanager before, you will need to migrate to use Afhttpsessionmanager. The following classes do not change between the two transitions:

    • SecurityPolicy
    • Requestserializer
    • Responseserializer

In the above description of the process has been demonstrated the latest version of the use, we can find that the HTTP network request returned is no longer afhttprequestoperation, modified into a nsurlsessiontask, and the parameters in the block blocks of success and failure are also changed to Nsurlsessiontask, not afhttprequestoperation.

Afhttprequestoperation Core Code
Unlike Nsurlconnection objects, each shared app-scoped setting such as session management, cache policy, cookie storage, and URL protocol, can be configured separately for each of these nsurlsession objects. Initializes a session with a specific configuration that can send tasks to fetch data and upload or download files.
In Afnetworking 2.0, using afhttprequestoperation, it is possible to create a separate network request with no additional overhead to obtain the data. Nsurlsession requires more overhead in order to obtain the data that is requested.
Next, you will create a singleton through Afhttpsessionmanager and create a task and start it.
Afnetworking 2.x

nsurl *url = [NSURL URLWithString : @ "" "; nsurlrequest *request = [nsurlrequest RequestWithURL:URL]; Afhttprequestoperation *op = [[Afhttprequestoperation alloc] Initwithrequest:request];op .responseserializer = [Afjsonresponseserializer serializer]; [Op setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, id Responseobject) {nslog (@ "JSON:%@", responseobject);} failure:^ (afhttprequestoperation *operation, nserror *error) { nslog (@ "Error:%@", error);}]; [[nsoperationqueue Mainqueue] addoperation:op];       

Afnetworking 3.0

NSURL *URL = [NSURL URLWithString:@""];AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];[manager GET:URL.absoluteString parameters:nil success:^(NSURLSessionTask *task, id responseObject) { NSLog(@"JSON: %@", responseObject);} failure:^(NSURLSessionTask *operation, NSError *error) { NSLog(@"Error: %@", error);}];

It is important to note that Nsurlsession is not built to rely on nsoperation. If you have an application that relies heavily on Afurlconnectionoperation's nsoperation, It may be necessary to re-construct the Nsoperation base method for your app's Afhttpsessionmanager.

Migration of Uikit
The picture download has been refactored to follow the Alamofireimage schema with the new Afimagedownloader class. This class of image download Agent is the UIButton and Uiimageview category, and provides some methods that can be customized if necessary. Category, the actual method of downloading the remote picture has not changed.
UIWebView's class is refactored to use Afhttpsessionmanager as its network request.

Uialertview's class was abandoned.
From Afnetworking 3.0, Uialertview's class was discarded due to obsolescence. does not provide a Uialertcontroller class purpose plan, because this is the logic that the application should handle, not this library.

Said so much, in fact, really simple to use, here is mainly about his improvement, our users are actually not so troublesome to use, just pay attention to now using the API based on Nsurlsession can be.

Part of this article is written in reference to the afnetworking documentation , which adds some personal understanding and explanations, some of which may be biased, please refer to the official instructions.

Afnetworking migration of New version 3.0

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: info-contact@alibabacloud.com 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.