封装AFNetWorking 3.0

  对AFNetWorking 3.0进行了封装,提供了一些常用方法,简化了使用。
  下载地址:git

使用 How to use

  将工程中的HttpUtils文件夹移入自己的项目。具体参数解释在注释中都有。

get

    NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"123" , @"id", nil];
    [Http getUrl:@"getMethod.php" parametersDic:dic success:^(NSDictionary *requestDic) {
        NSLog(@"部分URL get %@\n%@", requestDic,requestDic[@"name"]);
    } failure:^(NSError *errorInfo) {

    }];

post

    [Http postUrl:@"http://localhost:82/postMethod.php?id=123" parametersDic:nil success:^(NSDictionary *requestDic) {
        NSLog(@"%@", requestDic[@"name"]);
    } failure:^(NSError *errorInfo) {
        NSLog(@"failure :%@", errorInfo);
    }];

download

    [Http downLoadUrl:@"http://localhost:82/uploads/7096.wav" parametersDic:nil downLoadProgress:^(int64_t bytesRead, int64_t totalBytesRead) {
        NSLog(@"%lld",  bytesRead);
    } success:^(NSURL *filePath, NSURLResponse *response) {
        NSLog(@"download success %@", filePath);
    } failure:^(NSError *errorInfo) {
        NSLog(@"failure :%@", errorInfo);
    }];

upload

images
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    NSString *filePath = [docDir stringByAppendingPathComponent:@"80.jpg"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSMutableArray *imgArr = [[NSMutableArray alloc] init];
    [imgArr addObject:data];
    [imgArr addObject:data];
    [imgArr addObject:data];

    [Http postImagesData:imgArr uploadUrl:@"uploadMultipleImage.php" name:@"image[]" parametersDic:nil uploadProgress:^(int64_t bytesRead, int64_t totalBytesRead) {
        NSLog(@"%lld",  bytesRead);
    } success:^(NSDictionary *requestObj) {
        NSLog(@"upload images success :%@", requestObj);
    } failure:^(NSError *errorInfo) {
        NSLog(@"failure :%@", errorInfo);
    }];
voice
    [Http postVoiceData:voiceArr uploadUrl:@"uploadMultipleFile.php" name:@"file[]" mimeType:@"audio/x-wav" suffix:@"wav" parametersDic:nil uploadProgress:^(int64_t bytesRead, int64_t totalBytesRead) {
        NSLog(@"%lld",  bytesRead);
    } success:^(NSDictionary *requestObj) {
        NSLog(@"upload music success :%@", requestObj);
    } failure:^(NSError *errorInfo) {
        NSLog(@"failure :%@", errorInfo);
    }];
file(通过文件的data上传 Through the file data)
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    NSString *filePath = [docDir stringByAppendingPathComponent:@"80.jpg"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSMutableArray *filesDataArr = [NSMutableArray array];
    [filesDataArr addObject:data];
    [filesDataArr addObject:data];
    [filesDataArr addObject:data];

    [Http postFilesData:filesDataArr uploadUrl:@"uploadMultipleFile.php" name:@"file[]" suffix:nil parametersDic:nil uploadProgress:^(int64_t bytesRead, int64_t totalBytesRead) {
        NSLog(@"%lld",  bytesRead);
    } success:^(NSDictionary *requestObj) {
        NSLog(@"upload files success :%@", requestObj);
    } failure:^(NSError *errorInfo) {
        NSLog(@"failure :%@", errorInfo);    
    }];
file(通过文件在本地的路径上传 Through the file’s loacl path)
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    NSString *filePath = [docDir stringByAppendingPathComponent:@"80.jpg"];
    NSMutableArray *filesPathArr = [NSMutableArray array];
    [filesPathArr addObject:filePath];
    [filesPathArr addObject:filePath];
    [filesPathArr addObject:filePath];

    [Http postWithFilesPaths:filesPathArr uploadUrl:@"uploadMultipleFile.php" name:@"file[]" suffix:@"png" parametersDic:nil uploadProgress:^(int64_t bytesRead, int64_t totalBytesRead) {
        NSLog(@"%lld",  bytesRead);
    } success:^(NSDictionary *requestObj) {
        NSLog(@"upload files success :%@", requestObj);
    } failure:^(NSError *errorInfo) {
        NSLog(@"failure :%@", errorInfo);    
    }];

定制sessionManager

  通过定制sessionManager能够自定义请求头,设置请求格式以及返回格式等等。
  以上所有方法都提供了接受自定义的sessionManager的请求方法。

    UrlSessionManager *manager = [UrlSessionManager sharedManager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"value" forHTTPHeaderField:@"headerField"];
    [Http getUrl:@"http://yourUrl" parametersDic:nil sessionManager:manager success:^(NSDictionary *requestDic) {
        NSLog(@"%@", requestDic);        
    } failure:^(NSError *errorInfo) {

    }];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值