Download: Web site-GitHub
Two environments:
Libraries that need to be introduced-corelocation.framework
Systemconfiguration.framework
Mobilecoreservices.framework
Security.framework
Required in an ARC environment-non-ARC engineering-please add-FOBJC-ARC
Three structures:
1:afhttpclient-Provides a convenient network interface, including default headers, authentication, whether connected to the network, batch processing operations, query string parameter serialization, multiple form requests
2:afhttprequestoperation--
And its subclass can be based on the HTTP status and content column down to distinguish whether the request was successful
3:afurlconnectionoperation--
And its subclasses inherit Nsoperation, allowing requests to be canceled, paused/resumed, and managed by Nsoperationqueue.
4:afurlconnectionoperation--
Allows you to easily complete uploads and downloads, process validation, monitor upload and download progress, and control the cache.
Four uses:
Usage Scenario 1: Request API data
Method 1--
Create a Afhttpclient instance
afhttpclient * client = [[Afhttpclient alloc] initwithbaseurl:[nsurl urlwithstring:@ "http://api.jiepang.com"];
There must be a baseURL here--or an exception will be thrown--
Create a Nsurlrequest instance
Nsdictionary * params = @{@ "Apiver": @ "4", @ "id": @ "830755858", @ "Extra_info": @ "1"};
Nsurlrequest * request = [client requestwithmethod:@ "POST"
path:@ "Users/show"
Parameters:params];
@param method Network request, such as get,post,put,delete, cannot be nil--
The combination of @param path and BaseURL becomes a URL--in layman's words, the interface we call--for example, I want to call Http://api.jiepang.com/users/show, the interface--then BaseURL for HT Tp://api.jiepang.com--Path is users/show
@param parameters Network Request parameter--http://api.jiepang.com/v1/users/show?id=123--id=123 is this parameter
Create a Afhttprequestoperation instance for network links
afhttprequestoperation * operation = [client Httprequestoperationwithrequest:request success:^ ( Afhttprequestoperation *operation, id responseobject) {
NSLog (@ "success obj = =%@", responseobject);
} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
NSLog (@ "faild, error = =%@", error);
}];
[Operation start];
So-a simple call is OK-
Of course, it is possible to use other methods of encapsulation-
-(void) GetPath: (NSString *) path
Parameters: (nsdictionary *) parameters
Success: (void (^) (afhttprequestoperation *operation, id responseobject)) success
Failure: (void (^) (afhttprequestoperation *operation, Nserror *error)) failure;
-(void) Postpath: (NSString *) path
Parameters: (nsdictionary *) parameters
Success: (void (^) (afhttprequestoperation *operation, id responseobject)) success
Failure: (void (^) (afhttprequestoperation *operation, Nserror *error)) failure;
-(void) Putpath: (NSString *) path
Parameters: (nsdictionary *) parameters
Success: (void (^) (afhttprequestoperation *operation, id responseobject)) success
Failure: (void (^) (afhttprequestoperation *operation, Nserror *error)) failure;
-(void) Deletepath: (NSString *) path
Parameters: (nsdictionary *) parameters
Success: (void (^) (afhttprequestoperation *operation, id responseobject)) success
Failure: (void (^) (afhttprequestoperation *operation, Nserror *error)) failure;
-(void) Patchpath: (NSString *) path
Parameters: (nsdictionary *) parameters
Success: (void (^) (afhttprequestoperation *operation, id responseobject)) success
Failure: (void (^) (afhttprequestoperation *operation, Nserror *error)) failure;
Eliminates the time to assemble resquest-
such as
Method Two--
[Client Getpath:path
Parameters:params
success:^ (afhttprequestoperation *operation, id responseobject) {
NSString * obj = [[NSString alloc] Initwithdata:responseobject encoding:nsutf8stringencoding];
NSLog (@ "obj = =%@", obj);
} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
NSLog (@ "faild--");
}];
This is the same as the effect above--
We see that the data returned in the front is all nsdata--adding trouble to US--and most of our API return data is JSON--
Again-we can use the Afhttprequestoperation subclass afjsonrequestoperation to replace--
Method Three--
Nsdictionary * params = @{@ "Apiver": @ "4", @ "id": @ "830755858", @ "Extra_info": @ "1"};
afhttpclient * client = [[Afhttpclient alloc] initwithbaseurl:[nsurl urlwithstring:@ "http://api.jiepang.com"];
Nsurlrequest * request = [client requestwithmethod:@ "POST"
path:@ "Users/show"
Parameters:params];
afjsonrequestoperation * operation = [afjsonrequestoperation jsonrequestoperationwithrequest:request success:^ ( Nsurlrequest *request, nshttpurlresponse *response, id JSON) {
NSLog (@ "JSON = =%@", JSON);
} failure:^ (Nsurlrequest *request, Nshttpurlresponse *response, nserror *error, id JSON) {
NSLog (@ "faild--");
}];
[Operation start];
Use Scenario 2: Load pictures asynchronously
Afimagerequestoperation is inherited from Afhttprequestoperation--so the method is very similar--
NSString * url = @ "http://c.hiphotos.baidu.com/album/w=2048/sign=1d7ca85bac345982c58ae29238cc30ad/ F2deb48f8c5494ee7abe33362cf5e0fe99257e04.jpg ";
This is a big beauty.
Create request
Nsurlrequest * request = [nsurlrequest Requestwithurl:[nsurl Urlwithstring:url]
Cachepolicy:nsurlrequestreloadignoringlocalcachedata
TIMEOUTINTERVAL:30];
afimagerequestoperation * operation = [Afimagerequestoperation imagerequestoperationwithrequest:request
imageprocessingblock:^uiimage * (UIImage *image) {
uiimage * TempImage = [UIImage Imagewithcgimage:
Cgimagecreatewithimageinrect (image. Cgimage,ake (0, 0, image.size.width, image.size.height/2.0))];
return TempImage;
} success:^ (Nsurlrequest *request, NSHTTPURLResponse *response, UIImage *image) {
nslog (@ "Reload image success");
_imageview.image = image;
} failure:^ (Nsurlrequest *request, NSHTTPURLResponse *response, Nserror *error) {
nslog (@ "Reload image faild, error = =%@", Error);
}];
[Operation start];
There are three blocks in this method, success and failure do not say--processimageblock--is that after the picture is loaded, the block for the image processing can be nil, called before the success--
IOS using afnetworking