关于AFNetworking

AFNetworking 使用系列之 ~ 2.5.4 版本的一些封装

  • 使用block
typedef void(^RequestCompletionBlock) (id result, BOOL succeed);

#import <AFNetworking.h>
  • GET 请求
+ (void)commonRequestByGetWithURL:(NSString *)url parameters:(NSDictionary *)parameters completion:(RequestCompletionBlock)completion
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 30;
    [manager GET:url parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        completion(responseObject,YES);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        completion(error,NO);
    }];
    
}
  • POST请求
+ (void)commonRequestByPostWithURL:(NSString *)url parameters:(NSDictionary *)parameters completion:(RequestCompletionBlock)completion
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:@"text/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    manager.requestSerializer.timeoutInterval = 30;
    [manager POST:url parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        completion(responseObject,YES);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        completion(error,NO);
    }];
}

-上传图片

+ (void)commonUploadWithUrl:(NSString *)url image:(NSData *)imageData imageName:(NSString *)imageName completion:(RequestCompletionBlock)completion
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        NSString *newName = [NSString stringWithFormat:@"%@%@",imageName,@".png"];
        // 上传图片
        [formData appendPartWithFileData:imageData name:@"portrait" fileName:newName mimeType:@""];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        completion(responseObject,YES);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        completion(error,NO);
    }];
}

-下载

+ (void)commonDownloadWithUrl:(NSString *)url filePath:(NSURL *)filePath completion:(RequestCompletionBlock)completion
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
//    NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
    NSURL *URL = [NSURL URLWithString:url];
    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);
        if (error) {
            completion(filePath,NO);
        }else{
            completion(filePath,YES);
        }
    }];
    [downloadTask resume];
}

参考

AFNetworking

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 7,123评论 4 39
  • # Java 流式输入 输出原理 # Java 流的分类 # 输入 输出 流 类 # 常见的节点流 和 处理流 #...
    时待吾阅读 106评论 0 0
  • 完了,又没什么可写的了。最近每天都是1点多睡觉,估计长个是不可能的了。虽然是中考结束,想玩啥玩啥。可现在突然不想干...
    sesekoyite阅读 79评论 0 0
  • 一連幾天的大太陽,把這個青青島城變成了水煮“吐魯番”,記憶裏從未有過的熱,一遍遍刷新著紀錄。 午後...
    欢乐V英雄阅读 78评论 2 2