美文网首页
ios 原生网络请求封装

ios 原生网络请求封装

作者: 十国 | 来源:发表于2019-08-09 09:18 被阅读0次

    最新版本 => ios 原生网络请求封装(新)

    引用

    #import "Network.h"
    

    使用

    • get请求
        NSString * url = @"http://211.94.132.143:8090/api/AppMain/GetAppUserCardConfig";
        NSDictionary * dict = @{@"UserID" : @"cd168eec-620d-43fa-97a4-5aa497977068"};
    
        NSLog(@"%@",Network.get());
    
        
         Network
        .url(url)
        .params(dict)
        .done(^(NSDictionary * result,id error){
            NSLog(@"%@",result);
    
    
        });
    
    • 上传
     NSData * data1 =UIImagePNGRepresentation([UIImage imageNamed:@"xiaohuangren.png"]);
        NSData * data2 =UIImagePNGRepresentation([UIImage imageNamed:@"d.jpg"]);
        NSDictionary * dict = @{ @"RepairContent":@"上传图片测试22",
                                 @"Files":@[
                                         @{
                                             @"extname":@"png",
                                             @"data":data1
                                             },
                                         @{
                                             @"extname":@"jpg",
                                             @"data":data2
                                             }
                                         ]
                                 };
                               
        NSArray * headers =@[@{@"userID":@"3d6f00aa-5a87-41c8-8c02-0042ada0c9d2"}];
        Network
        .upload()
        .headers(headers)
        .url(@"http://192.168.0.200:8080/api/Base_Attachment/Upload")
        .params(dict)
        .progress(^(float fraction,int64_t completed,int64_t total){
            printf("\n进度%f   已发送%lld   总量%lld",fraction,completed,total);
            self.progressView.progress = fraction;
        })
        .done(^(NSDictionary * result,id error){
            NSLog(@"上传返回结果:%@",result);
            NSLog(@"上传失败:%@",error);
        });
    
    • 下载
        NSDictionary * dict =@{@"userID":@"fc39abca-8a0d-46d1-bb42-5e0abf9e386c",
                               @"docID":@"f9b3c8d0-7524-4312-8c90-8b64e92e20bb",
                               @"Token":@"A0DC5DB9174BB0F5893FDA55D0796E6DB28532CC4C73C384ADB84E0E8ABFA31FDA784F79ABEC4C187586D950171E34DB19EE66506930E849",
                               
                               };
        NSString * url = @"http://www.htkj-bj.com/cocp/api/Documents/GetDocDownLoad";
         Network
        .download()
        .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;
        })
        .file(^(NSURL * localUrl){
            printf("\n下载完成");
            NSLog(@"%@",localUrl);
            //NSData * data = [NSData dataWithContentsOfURL:localUrl];
        })
        .done(^(NSError * error){
            if (!error) {
                printf("\n请求成功");
            }else{
                printf("\n请求失败");
            }
        });
    

    下载地址

    相关文章

      网友评论

          本文标题:ios 原生网络请求封装

          本文链接:https://www.haomeiwen.com/subject/isfcjctx.html