I. Introduction of Afnetworking
- Afnetworking is a very many open source libraries used in iOS development
- For iOS and Mac OS x. It is built on top of (Apple iOS development documentation) Nsurlconnection, Nsoperation, and other familiar foundation technologies.
- It has a good architecture, a rich API, and a modular way of building it, making it easy to use.
- Official link http://cocoadocs.org/docsets/AFNetworking/1.3.0/.
- Afnetworking is a lightweight network Request API Class library. is based on Nsurlconnection, Nsoperation and other methods.
- Core code: Afhttprequestoperationmanager.
- Afnetworking has a good architecture, rich APIs, and a modular way of building it, making it easy to use.
- AFNETWORKING3.0 currently uses nsurlsession as the network class.
Second, afnetworking function carding
- Afurlconnectionoperation: Inherited from Nsoperation implements the Nsurlconnection proxy method.
- Afhttprequestoperation: A subclass inherited from Afurlconnectionoperation that is used when the protocol requested by request is HTTP and HTTPS, which encapsulates the The status code and content type that determines whether the request is successful.
- Afjsonrequestoperation: Inherits from Afhttprequestoperation, which is used to download and process JSON response data.
- Afxmlrequestoperation: Inherits from Afhttprequestoperation, which is used to download and process XML response data.
- Afpropertylistrequestoperation: Inherits from Afhttprequestoperation, which is used to download and process the property list response data.
- Afhttpclient: is a public communication mode that encapsulates a Web application based on an HTTP protocol. Contains:
- Initiates the use of a basic URL-dependent path based on the root path to do only the request.
- Set HTTP headers for request auto-Add.
- Use the HTTP base certificate or OAuth to verify the request.
- Manage a nsoperationqueue for requests created by client.
- Generates a query string or HTTP bodies from Nsdictionary.
- Build multiple parts from the request.
- Automatic parsing of HTTP response data for corresponding performance data.
- Monitoring and response changes during network accessibility testing.
Note : afnetworking can perform JSON data parsing/plist data parsing. (XML data parsing is not supported.) If there is a special character or Chinese characters in the URL string, afnetworking does not do UTF8 transcoding, need: url = [url stringbyaddingpercentescapesusingencoding: Nsutf8stringencoding];
Third, code example
Import afnetworking with Cocoapods (Create a button with storyboard and associate the event):
#import "ViewController.h"//header file for network requests#import<AFNetworking.h>#import<AFNetworkActivityIndicatorManager.h>@interfaceViewcontroller () {///the bool value of the network monitoring JudgmentBOOL IsOpen;}///Session object for network requests@property (nonatomic, strong) Afhttpsessionmanager *session;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //Initialize Session objectSelf.session =[Afhttpsessionmanager Manager]; //set what type of data is supported when the request interface comes backSelf.session.responseSerializer.acceptableContentTypes = [Nsset setwithobjects:@"Application/json",@"Text/json",@"Text/javascript",@"Application/x-json",@"text/html", nil]; }#pragmaMakr-Response method of the Network Monitor button-(ibaction) Networkmonitoringaction: (ID) Sender {if(!IsOpen) { //Turn on network monitoring[[Afnetworkreachabilitymanager Sharedmanager] startmonitoring]; IsOpen=YES; }Else { //Turn off network monitoring[[Afnetworkreachabilitymanager Sharedmanager] stopmonitoring]; IsOpen=NO; } //Next, you will determine whether the current WiFi or 3G State or network unavailable state[[Afnetworkreachabilitymanager Sharedmanager] Setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) {Switch(status) { CaseAfnetworkreachabilitystatusunknown:nslog (@"The current network is in an unknown state"); Break; CaseAfnetworkreachabilitystatusnotreachable:nslog (@"The current network is in an disconnected state"); Break; CaseAfnetworkreachabilitystatusreachableviawwan:nslog (@"3G or 4G network"); Break; CaseAfnetworkreachabilitystatusreachableviawifi:nslog (@"WiFi"); Break; default: Break; } }];}#pragmaMark-get Request-(Ibaction) Getrequestaction: (IDSender {[self.session GET:@"http://api.yhouse.com/m/city/dynmiclist"Parameters:nil progress:^ (nsprogress *_nonnull downloadprogress) {NSLog (@"the progress bar =%@", downloadprogress); } Success:^ (Nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) {NSLog (@"Request succeeded:%@", Responseobject); } Failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {NSLog (@"request failed%@", error); }];}#pragmaMark-post Request-(Ibaction) Postrequestaction: (ID) Sender {/*{do = "pri_memberlist"; "member_id" = Zphr2dsrvqqxyjxo2; "workspace_id" = ilfype4dhs2gwcuqx; }*/[Afnetworkactivityindicatormanager sharedmanager].enabled=YES; [[UIApplication sharedapplication] setnetworkactivityindicatorvisible:yes]; NSString*urlstr =@"http://m.taskwedo.com/API/wedo1/wedo.php"; Nsmutabledictionary*dict =[Nsmutabledictionary dictionary]; [Dict setValue:@"pri_memberlist"Forkey:@" Do"]; [Dict setValue:@"Zphr2dsrvqqxyjxo2"Forkey:@"member_id"]; [Dict setValue:@"ILFYPE4DHS2GWCUQX"Forkey:@"workspace_id"]; [Self.session post:urlstr parameters:dict Progress:^ (Nsprogress *_nonnull uploadprogress) {NSLog (@"Upload progress =%@", uploadprogress); } Success:^ (Nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) {NSLog (@"POST Request succeeded =%@", Responseobject); } Failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {NSLog (@"request failed =%@", error); }];}#pragmaMark Post Request-(ibaction) Post2requestaction: (ID) Sender {/*address = ""; Comment = "\u7c7b\u6a21\u5757\u8ba1\u5212\u7528\u5230\u7b2c\u4e09\u90e8\u5206\u4e2d\uff0c\u5f85\u63d0\u95ee\ U3001\u56de\u7b54\u79ef\u7d2f\u5230\u4e00\u5b9a\u6570\u91cf\u65f6\uff0c\u4fbf\u4e8e\u5927\u5bb6\u7684\u95ee\ U9898\u7684\u5feb\u901f\u67e5\u627e\uff0c\u6240\u4ee5\u63d0\u95ee\u90e8\u5206\u6682\u65f6\u4e0d\u52a0\u5165\ U8FD9\U4E2A "; do = "add_comment"; kind = task; "member_id" = Zphr2dsrvqqxyjxo2; other = ""; "task_id" = 55a47e79ec25e3641;*/NSString*urlstr =@"http://m.taskwedo.com/API/wedo1/wedo.php"; NSString*commoncontent =@"class module planning to use the third part, to ask questions, answers accumulated to a certain number, easy to find a quick question, so the question part of the temporary not to join this"; Commoncontent=[Commoncontent Stringbyaddingpercentencodingwithallowedcharacters:[nscharacterset Urlqueryallowedcharacterset]]; Nsmutabledictionary*dic =[Nsmutabledictionary dictionary]; [DiC SetValue:@""Forkey:@"Address"]; [DiC setvalue:commoncontent Forkey:@"Comment"]; [DiC SetValue:@"add_comment"Forkey:@" Do"]; [DiC SetValue:@"Task"Forkey:@"Kind"]; [DiC SetValue:@"Zphr2dsrvqqxyjxo2"Forkey:@"member_id"]; [DiC SetValue:@""Forkey:@" Other"]; [DiC SetValue:@"55a47e79ec25e3641"Forkey:@"task_id"]; [Self.session post:urlstr parameters:dic Progress:^ (Nsprogress *_nonnull uploadprogress) {NSLog (@"Upload progress =%@", uploadprogress); } Success:^ (Nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) {NSLog (@"POST Request succeeded =%@", Responseobject); } Failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {NSLog (@"request failed =%@", error); }];}@end
Advanced iOS Learning-first Knowledge afnetworking