A brief introduction to AFN
Afnetworking is a favorite network library in OS X and iOS.
To cater for the new iOS version of the upgrade, afnetworking removed all support based on the Nsurlconnection API in version 3.0.
If your project has previously used these APIs, it is recommended that you upgrade to the afnetworking version of the Nsurlsession-based API immediately. This guide will guide you through this process.
This chapter is intended to guide the use of Afnetworking 2.x to upgrade to the latest version of the API, to achieve the purpose of the transition, and to explain the new and changed design structure.
New device Requirements: IOS 7, Mac OS X 10.9, WatchOS 2, TvOS 9, & Xcode 7
Afnetworking 3.0 officially supports iOS 7, Mac OS x 10.9, WatchOS 2, TvOS 9, and Xcode 7. If you want to use afnetworking for an older version of the SDK project, please check the Readme compatibility information.
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 while supporting Apple to enhance any additional functionality offered by Nsurlsession. Because of Xcode 7, Nsurlconnection's API has officially been 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. Alamofire (Network request under Swift) The Software Foundation recommends that all projects be migrated to the Nsurlsession-based API.
Deprecated classes
The following classes have been deprecated from Afnetworking 3.0:
Afurlconnectionoperation
Afhttprequestoperation
Afhttprequestoperationmanager
The Modified class
The following class contains an internal implementation of the Nsurlconnection-based API. They have been used nsurlsession refactoring:
Uiimageview+afnetworking
Uiwebview+afnetworking
Uibutton+afnetworking
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
Next, let's take a simple example of Afhttpsessionmanager. Note that the HTTP network request returned is no longer afhttprequestoperation, the modification became nsurlsessiontask, and the parameters in the successful and failed block block changed to Nsurlsessiontask, And no longer a afhttprequestoperation.
Afnetworking 2.x
Afhttprequestoperationmanager *manager = [Afhttprequestoperationmanager manager];
[Manager get:@ "requested URL" Parameters:nil success:^ (afhttprequestoperation *operation, id responseobject) {
NSLog (@ "Success");
} failure:^ (Afhttprequestoperation *operation, Nserror*error) {
NSLog (@ "failed");
}];
Afnetworking 3.0
Afhttpsessionmanager *session = [Afhttpsessionmanager manager];
[Session get:@ "requested URL" Parameters:nil success:^ (nsurlsessiondatatask *task, id responseobject) {
NSLog (@ "Success");
} failure:^ (Nsurlsessiondatatask *task, Nserror *error) {
NSLog (@ "failed");
}];
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);
}];
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.
`
iOS AFN Introduction (1)