官網下載2.5版本:http://afnetworking.com/php
此文章是基於AFNetworking2.5版本的,須要看AFNetworking2.0版本的請看上一篇文章:AFNetworking2.0使用html
1.檢測網絡狀態程序員
- + (void)netWorkStatus
- {
-
-
- [[AFNetworkReachabilityManager sharedManager] startMonitoring];
-
-
- [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
- NSLog(@"%ld", status);
- }];
- }
2.JSON方式獲取數據json
- + (void)JSONDataWithUrl:(NSString *)url success:(void (^)(id json))success fail:(void (^)())fail;
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
- NSDictionary *dict = @{@"format": @"json"};
-
- [manager GET:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
- if (success) {
- success(responseObject);
- }
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- if (fail) {
- fail();
- }
- }];
- }
3.xml方式獲取數據xcode
- + (void)XMLDataWithUrl:(NSString *)urlStr success:(void (^)(id xml))success fail:(void (^)())fail
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
-
- NSDictionary *dict = @{@"format": @"xml"};
-
-
- [manager GET:urlStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
- if (success) {
- success(responseObject);
- }
-
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- if (fail) {
- fail();
- }
- }];
- }
4.post提交json數據緩存
- + (void)postJSONWithUrl:(NSString *)urlStr parameters:(id)parameters success:(void (^)(id responseObject))success fail:(void (^)())fail
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
- manager.requestSerializer = [AFJSONRequestSerializer serializer];
-
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
- [manager POST:urlStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
- if (success) {
- success(responseObject);
- }
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- if (fail) {
- fail();
- }
- }];
-
- }
5.下載文件服務器
- + (void)sessionDownloadWithUrl:(NSString *)urlStr success:(void (^)(NSURL *fileURL))success fail:(void (^)())fail
- {
- NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
- AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:config];
-
- NSString *urlString = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
-
- NSURL *url = [NSURL URLWithString:urlString];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
-
- NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
-
-
-
- NSString *cacheDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
- NSString *path = [cacheDir stringByAppendingPathComponent:response.suggestedFilename];
-
-
- NSURL *fileURL = [NSURL fileURLWithPath:path];
-
- if (success) {
- success(fileURL);
- }
-
- return fileURL;
- } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
- NSLog(@"%@ %@", filePath, error);
- if (fail) {
- fail();
- }
- }];
-
- [task resume];
- }
6.文件上傳-自定義上傳文件名網絡
- + (void)postUploadWithUrl:(NSString *)urlStr fileUrl:(NSURL *)fileURL fileName:(NSString *)fileName fileType:(NSString *)fileTye success:(void (^)(id responseObject))success fail:(void (^)())fail
- {
-
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
-
- [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
-
-
-
-
-
-
-
- [formData appendPartWithFileURL:fileURL name:@"uploadFile" fileName:fileName mimeType:fileTye error:NULL];
-
- } success:^(AFHTTPRequestOperation *operation, id responseObject) {
- if (success) {
- success(responseObject);
- }
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- if (fail) {
- fail();
- }
- }];
- }
7.文件上傳-隨機生成文件名session
- + (void)postUploadWithUrl:(NSString *)urlStr fileUrl:(NSURL *)fileURL success:(void (^)(id responseObject))success fail:(void (^)())fail
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
-
-
-
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
-
-
- [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
-
-
-
- [formData appendPartWithFileURL:fileURL name:@"uploadFile" error:NULL];
- } success:^(AFHTTPRequestOperation *operation, id responseObject) {
- if (success) {
- success(responseObject);
- }
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"錯誤 %@", error.localizedDescription);
- if (fail) {
- fail();
- }
- }];
- }