IOS network analysis-(Introduction to third-party AFNetworking3.0 on November 11), iosafnetworking3.0
AFNetworking3.0 is the latest version. I was going to introduce it to 2.6, but I decided not to introduce it soon. Interested friends can check it online. Now I start to get started.
Currently, no third-party network library is used most frequently. From the beginning of NSURLConnection to the current NSURLSession, it has been consistent with Apple's pace, and it also produces a large number of related third-party network function libraries, not only because of its reliability, it is easy to use and maintains updates, which is why it is favored by the majority of programmers.
Upload data
/// ViewController. m // CX-AFNetworking3.0 overview /// Created by ma c on 16/3/22. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "# import" AFNetworking. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self upLoadData];} // upload data-(void) upLoadData {UIImage * image = [UIImage imageNamed: @ "renminbi.jpg"]; NSData * data = UIImageJPEGRepresentation (image, 1); AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; /* The POST upload address parameters text parameter constructingBodyWithBlock indicates the block of the file to be uploaded. It is possible to call the progress upload progress multiple times. success upload is successful. failure upload fails. */[manager POST: @ "http: // localhost/post/upload. php "parameters: nil constructingBodyWithBlock: ^ (id <AFMultipartFormData> _ Nonnull formData) {/* data uploaded by formData FileData the data name of the uploaded file the name on the key fileName server of the uploaded file mimeType the type of the uploaded resource */[formData appendPartWithFileData: data name: @ "userfile00" fileName: @ "xubaoaichiyu" mimeType: @ "image/jpg"];} progress: ^ (NSProgress * _ Nonnull uploadProgress) {NSLog (@ "% @", uploadProgress);} success: ^ (NSURLSessionDataTask * _ Nonnull task, id _ Nullable responseObject) {NSLog (@ "success-> % @", responseObject);} failure: ^ (NSURLSessionDataTask * _ Nullable task, NSError * _ Nonnull error) {NSLog (@ "failed-> % @", error) ;}@ end
Upload files
// Upload the file AFHTTPSessionManager * manager = [AFHTTPSessionManager];/* block of the uploaded file using the POST upload address parameters text parameter constructingBodyWithBlock, it is possible to call progress upload progress multiple times success upload succeeded failure upload Failed */[manager POST: @ "http: // localhost/post/upload. php "parameters: nil constructingBodyWithBlock: ^ (id <AFMultipartFormData> _ Nonnull formData) {/* data uploaded by formData FileData the data name of the uploaded file the mimeType name on the key fileName server of the uploaded file the type of the uploaded resource */NSURL * url = [[NSBundle mainBundle] URLForResource: @ "renminbi.jpg" withExtension: nil]; [formData appendPartWithFileURL: url name: @ "userfile00" fileName: @ "xubaoaichiyu" mimeType: @ "image/jpg" error: nil];} progress: ^ (NSProgress * _ Nonnull uploadProgress) {NSLog (@ "% @", uploadProgress);} success: ^ (NSURLSessionDataTask * _ Nonnull task, id _ Nullable responseObject) {NSLog (@ "success-> % @", responseObject);} failure: ^ (NSURLSessionDataTask * _ Nullable task, NSError * _ Nonnull error) {NSLog (@ "failed-> % @", error) ;}];
Get
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; [manager GET:@"http://localhost/post/upload.php" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];
Post
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; [manager POST:@"http://localhost/post/upload.php" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];
GetXML
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; manager.requestSerializer = [AFHTTPRequestSerializer serializer]; manager.responseSerializer = [AFXMLParserResponseSerializer serializer]; [manager GET:@"http://localhost/post/upload.php" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];