iOS development afnetworking use of open source Libraries 1. Introduction
Common ways to handle network requests
(1) Nsurlconnection/nsurlsession
(2) ASIHTTPRequest early projects are more common, as authors do not update the
(3) Afnetworking recommended for use in projects
(4) Mknetworkkit try it.
2. Configuring and Using 2.1 configurations
Library files are dragged into the project, including header files
#import "AFNetworking.h"
2.2 Use
//demonstrating the use of afnetworking//1. Get Request (Html,json,xml)//[self testgetrequset]; //2.POST Request//[self testpostrequest]; //3. Uploading files (uploading images)//[self testuploadfile]; //4. Download the file//[self testdownloadfile]; //5. Monitor network status[self testmonitornetworkstatus]; //6. Image Async Function (replace Sdwebimage)//uikit+afnetworking.h//-(void) Setimagewithurl: (nsurl *) URL; //7. Some requests for special request headers//baidu_wise_uid=wapp_1428385381699_466; //Afhttprequestoperationmanager *manager = [Afhttprequestoperationmanager manager]; //[Manager.requestserializer setvalue:@ "wapp_1428385381699_466" forhttpheaderfield:@ "BAIDU_WISE_UID"]; }-(void) testmonitornetworkstatus{Afhttprequestoperationmanager*manager = [[Afhttprequestoperationmanager alloc] Initwithbaseurl:[nsurl urlwithstring:@"www.baidu.com"]]; [Manager.reachabilitymanager Setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) {nsdictionary*dict =@{@ (afnetworkreachabilitystatusunknown):@"Unknown", @ (afnetworkreachabilitystatusnotreachable):@"not reached", @ (Afnetworkreachabilitystatusreachableviawwan):@"GPRS", @ (Afnetworkreachabilitystatusreachableviawifi):@"Wifi",}; NSLog (@"status is%@", dict[@ (status)]); }]; //Turn on Status monitoring[Manager.reachabilitymanager startmonitoring]; }-(void) testdownloadfile{NSString*urlstring =@"Http://imgcache.qq.com/club/item/avatar/zip/7/i87/all.zip"; //Create a Session Manager object (via default configuration)Afurlsessionmanager *manager =[[Afurlsessionmanager alloc] Initwithsessionconfiguration:[nsurlsessionconfiguration Defaultsessionconfiguration]]; Nsurlsessiondownloadtask*task = [Manager downloadtaskwithrequest:[nsurlrequest Requestwithurl:[nsurl Urlwithstring:urlstring]] Progress:nil Destination:^nsurl * (Nsurl *targetpath, Nsurlresponse *response) { //return file Save locationNSString *path = [NSString stringWithFormat:@"%@/documents/all.zip", Nshomedirectory ()]; NSLog (@"Path =%@", path); return[Nsurl Fileurlwithpath:path]; } Completionhandler:^ (Nsurlresponse *response, Nsurl *filepath, Nserror *error) {NSLog (@"Download Complete"); }]; //Start a task[Task resume]; }-(void) testuploadfile{//Post Upload InterfaceNSString *urlstring =@"http://quiet.local/uploadtest/upload.php"; //Parameter name: Image: Parameter value is a pictureAfhttprequestoperationmanager *mamager =[Afhttprequestoperationmanager Manager]; Mamager.responseserializer=[Afhttpresponseserializer Serializer]; [Mamager post:urlstring parameters:nil Constructingbodywithblock:^(ID<AFMultipartFormData>formData) { //implementation: The uploaded data is attached to the request body//MimeType Multi-purpose Internet Mail extension type, each file has a typeNSString *path = [[NSBundle mainbundle] Pathforresource:@"img_4131.jpg"Oftype:nil]; NSLog (@"Path =%@", path); [FormData Appendpartwithfileurl:[nsurl Fileurlwithpath:path] Name:@"Image"FileName:@"quiet1.jpg"MimeType:@"Image/jpeg"Error:nil]; } Success:^ (Afhttprequestoperation *operation,IDresponseobject) {NSString*str =[[NSString alloc] Initwithdata:responseobject encoding:nsutf8stringencoding]; NSLog (@"str =%@", str); } Failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@"error =%@", error); }]; }-(void) testpostrequest{//Post Interface:http://quiet.local/testdir/login.php //parameter 1: @ "user": @ "quiet"//parameter 2: @ "password": @ "123"NSString *urlstring =@"http://quiet.local/testdir/login.php"; Afhttprequestoperationmanager*mamager =[Afhttprequestoperationmanager Manager]; Mamager.responseserializer=[Afhttpresponseserializer Serializer]; //parameter 1: Incoming address//parameter 2: Incoming URL request parameter, format passed in dictionary[Mamager post:urlstring parameters:@{@"User":@"Quiet",@"Password":@"123"} success:^ (Afhttprequestoperation *operation,IDresponseobject) {NSString*str =[[NSString alloc] Initwithdata:responseobject encoding:nsutf8stringencoding]; NSLog (@"str =%@", str); } Failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@"error =%@", error); }]; }-(void) testgetrequset{NSString*urlstring =@"http://www.baidu.com"; URLString=@"http://m.weather.com.cn/data/101010100.html"; //Define afnetworking ManagementAfhttprequestoperationmanager *manager =[Afhttprequestoperationmanager Manager]; //Note: The default is that the data is Json,content-type and JSON, Responseobject is a parsed dictionary and an array, and is not generating an error code=-1016//FIX: Set parser to HTTP form, download back is NSDataManager.responseserializer =[Afhttpresponseserializer Serializer]; [Manager get:urlstring Parameters:nil success:^ (Afhttprequestoperation *operation,IDresponseobject) { //responseobject important parameters, including download data//NSLog (@ "o =%@", responseobject);NSString *str =[[NSString alloc] Initwithdata:responseobject encoding:nsutf8stringencoding]; NSLog (@"str =%@", str); } Failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@"error =%@", error); }];}
Use of the afnetworking Open Source Library for iOS development