准备
首先,你需要将afnetworking 框架包含到工程中。如果你还没有afnetworking的话,在这里下载最新的版本:
https://github.com/afnetworking/afnetworking
当你解压出下载的文件后,你将看到其中有一个afnetworking子文件夹,里面全是.h 和 .m 文件, 如下高亮显示的:
将afnetworking拖拽到xcode工程中.
当出现了添加文件的选项时,确保勾选上copy items into destination group's folder (if needed) 和 create groups for any added folders.
将afnetworking添加到预编译头文件,意味着这个框架会被自动的添加到工程的所有源代码文件中。
常用方法介绍
方法一:get 请求
nsurl *url = [nsurl urlwithstring:@"http://example.com/download.zip"];
nsurlrequest *request = [nsurlrequest requestwithurl:url];
nsurlsessiondownloadtask *downloadtask = [manager downloadtaskwithrequest:request progress:nil destination:^nsurl *(nsurl *targetpath, nsurlresponse *response) {
nsurl *documentsdirectoryurl = [[nsfilemanager defaultmanager] urlfordirectory:nsdocumentdirectory indomain:nsuserdomainmask appropriateforurl:nil create:no error:nil];
return [documentsdirectoryurl urlbyappendingpathcomponent:[response suggestedfilename]];
} completionhandler:^(nsurlresponse *response, nsurl *filepath, nserror *error) {
nslog(@"file downloaded to: %@", filepath);
}];
[downloadtask resume];
nsurl *url = [nsurl urlwithstring:@"http://example.com/upload"];
nsurlrequest *request = [nsurlrequest requestwithurl:url];
nsurl *filepath = [nsurl fileurlwithpath:@"file://path/to/image.png"];
nsurlsessionuploadtask *uploadtask = [manager uploadtaskwithrequest:request fromfile:filepath progress:nil completionhandler:^(nsurlresponse *response, id responseobject, nserror *error) {
if (error) {
nslog(@"error: %@", error);
} else {
nslog(@"success: %@ %@", response, responseobject);
}
}];
[uploadtask resume];
afurlsessionmanager *manager = [[afurlsessionmanager alloc] initwithsessionconfiguration:[nsurlsessionconfiguration defaultsessionconfiguration]];
nsprogress *progress = nil;
nsurlsessionuploadtask *uploadtask = [manager uploadtaskwithstreamedrequest:request progress:&progress completionhandler:^(nsurlresponse *response, id responseobject, nserror *error) {
if (error) {
nslog(@"error: %@", error);
} else {
nslog(@"%@ %@", response, responseobject);
}
}];
[uploadtask resume];
nsurl *url = [nsurl urlwithstring:@"http://example.com/upload"];
nsurlrequest *request = [nsurlrequest requestwithurl:url];
nsurlsessiondatatask *datatask = [manager datataskwithrequest:request completionhandler:^(nsurlresponse *response, id responseobject, nserror *error) {
if (error) {
nslog(@"error: %@", error);
} else {
nslog(@"%@ %@", response, responseobject);
}
}];
[datatask resume];
nsoperationqueue *operationqueue = manager.operationqueue;
[manager.reachabilitymanager setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) {
switch (status) {
case afnetworkreachabilitystatusreachableviawwan:
case afnetworkreachabilitystatusreachableviawifi:
[operationqueue setsuspended:no];
break;
case afnetworkreachabilitystatusnotreachable:
default:
[operationqueue setsuspended:yes];
break;
}
}];
[manager.reachabilitymanager startmonitoring];
afhttprequestoperation *operation = [[afhttprequestoperation alloc] initwithrequest:request];
[mutableoperations addobject:operation];
}
nsarray *operations = [afurlconnectionoperation batchofrequestoperations:@[...] progressblock:^(nsuinteger numberoffinishedoperations, nsuinteger totalnumberofoperations) {
nslog(@"%lu of %lu complete", numberoffinishedoperations, totalnumberofoperations);
} completionblock:^(nsarray *operations) {
nslog(@"all operations in batch complete");
}];
[[nsoperationqueue mainqueue] addoperations:operations waituntilfinished:no];
request serializers create requests from url strings, encoding parameters as either a query string or http body.
nsstring *urlstring = @"http://example.com";
nsdictionary *parameters = @{@"foo": @"bar", @"baz": @[@1, @2, @3]};
query string parameter encoding
[[afhttprequestserializer serializer] requestwithmethod:@"get" urlstring:urlstring parameters:parameters error:nil];
get http://example.com?foo=bar&baz[]=1&baz[]=2&baz[]=3
url form parameter encoding
[[afhttprequestserializer serializer] requestwithmethod:@"post" urlstring:urlstring parameters:parameters];
post http://example.com/
content-type: application/x-www-form-urlencoded
foo=bar&baz[]=1&baz[]=2&baz[]=3
json parameter encoding
[[afjsonrequestserializer serializer] requestwithmethod:@"post" urlstring:urlstring parameters:parameters];
post http://example.com/
content-type: application/json
{"foo": "bar", "baz": [1,2,3]}
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
网友评论