赞
踩
AFNetworking是一个为
4.UIKit扩展
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationMa nager manager];[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject);} failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error);}];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationMa nager manager];NSDictionary *parameters = @{@"foo": @"bar"};[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject);} failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error);}];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationMa nager manager];NSDictionary *parameters = @{@"foo": @"bar"};NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBloc k:^(id formData) { [formData appendPartWithFileURL:filePath name:@"image" error:nil];} success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: %@", responseObject);} failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error);}];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguratio n defaultSessionConfigurat ion];AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfigura tion:configuration];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 URLByAppendingPathCompon ent:[response suggestedFilename]];} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { NSLog(@"File downloaded to: %@", filePath);}];[downloadTask resume];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguratio n defaultSessionConfigurat ion];AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfigura tion:configuration];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];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBloc k:^(id formData) { [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil]; } error:nil];AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfigura tion:[NSURLSessionConfiguratio n defaultSessionConfigurat ion]];NSProgress *progress = nil;NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRe quest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { if (error) { NSLog(@"Error: %@", error); } else { NSLog(@"%@ %@", response, responseObject); }}];[uploadTask resume];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguratio n defaultSessionConfigurat ion];AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfigura tion:configuration];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];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusCha ngeBlock:^(AFNetworkReachabilitySta tus status) { NSLog(@"Reachability: %@", AFStringFromNetworkReach abilityStatus(status));}];
NSURL *baseURL = [NSURL URLWithString:@"http://example.com/"];AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationMa nager alloc] initWithBaseURL:baseURL];NSOperationQueue *operationQueue = manager.operationQueue;[manager.reachabilityManager setReachabilityStatusCha ngeBlock:^(AFNetworkReachabilitySta tus status) { switch (status) { case AFNetworkReachabilitySta tusReachableViaWWAN: case AFNetworkReachabilitySta tusReachableViaWiFi: [operationQueue setSuspended:NO]; break; case AFNetworkReachabilitySta tusNotReachable: default: [operationQueue setSuspended:YES]; break; }}];
它封装的获取后的HTTP状态和类型将决定请求的成功与否。虽然AFHTTPRequestOperationMa
NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.json"];NSURLRequest *request = [NSURLRequest requestWithURL:URL];AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];op.responseSerializer = [AFJSONResponseSerializerserializer];[op setCompletionBlockWithSu ccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject);} failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error);}];[[NSOperationQueue mainQueue] addOperation:op];
NSMutableArray *mutableOperations = [NSMutableArray array];for (NSURL *fileURL in filesToUpload) { NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBloc k:^(id formData) { [formData appendPartWithFileURL:fileURL name:@"images[]" error:nil]; }]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [mutableOperations addObject:operation];}NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations :@[...] progressBlock:^(NSUInteger numberOfFinishedOperatio ns, NSUInteger totalNumberOfOperations) { NSLog(@"%lu of %lu complete", numberOfFinishedOperatio ns, totalNumberOfOperations);} completionBlock:^(NSArray *operations) { NSLog(@"All operations in batch complete");}];[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。