ios 原生网络请求封装(新)

引用

#import "Network.h"

按需组合

  • get方法(例子:简易使用)
     Network
    .url(url)
    .params(dict)
    .get(^(NSDictionary * result,NSError * error){
        NSLog(@"%@",result);


    });
  • post方法(例子:包含请求头、url编码)
     Network
    .url(url)
    .params(dict)
    .headers(headers)
    .urlencoded()
    .post(^(NSDictionary * result,NSError * error){
        NSLog(@"%@",result);


    });
  • upload方法(例子:包含上传进度、多个文件上传)
    NSData * data1 =UIImagePNGRepresentation([UIImage imageNamed:@"xiaohuangren.png"]);
    NSData * data2 =UIImagePNGRepresentation([UIImage imageNamed:@"d.jpg"]);
    NSDictionary * dict = @{ @"RepairContent":@"上传图片测试22",
                             @"Files":@[
                                     @{
                                         @"type":@"png",
                                         @"data":data1
                                         },
                                     @{
                                         @"type":@"jpg",
                                         @"data":data2
                                         }
                                     ]
                             };

    NSArray * headers =@[@{@"userID":@"3d6f00aa-5a87-41c8-8c02-0042ada0c9d2"}];
    Network
    .url(@"http://192.168.0.200:8080/api/Base_Attachment/Upload")
    .headers(headers)
    .params(dict)
    .progress(^(float fraction,int64_t completed,int64_t total){
        printf("\n进度%f   已发送%lld   总量%lld",fraction,completed,total);
        self.progressView.progress = fraction;
    })
    .upload(^(NSDictionary * result,NSError * error){
        NSLog(@"上传返回结果:%@",result);
        NSLog(@"上传失败:%@",error);
    });
  • download方法
     Network
    .url(url)
    .params(dict)
    .progress(^(float fraction,int64_t completed,int64_t total){
        printf("\n进度%f   已接收%lld   总量%lld",fraction,completed,total);
        self.progressView.progress = fraction;
    })
    .download(^(NSURL * localUrl,NSError * error){
        if (!error) {
            printf("\n下载成功!\n");
            printf([[localUrl absoluteString] UTF8String],nil);
            //NSData * data = [NSData dataWithContentsOfURL:localUrl];
        }else{
            printf("\n下载失败!");
        }
    });

下载地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容