AFNetworking施工例子

Developers targeting iOS 7 or Mac OS X 10.9 or later that deal extensively with a web service are encouraged to subclass `AFHTTPSessionManager`, providing a class method that returns a shared singleton object on which authentication and other configuration can be shared across the application.

看原文作者要求提到提供一个类方法返回一个单例对象那么就写个单例提供类方法

+(instancetype)shareManager{
    static dispatch_once_t onceToken;
    static Manager *instace;
    dispatch_once(&onceToken, ^{
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        //超时时间设定
        config.timeoutIntervalForRequest = 15;
        //序列化
        instace.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",@"plant/html", nil];
        instace = [[self alloc]initWithBaseURL:BaseURL];
    });
    return instace;
}

BaseURL 是用define 设定的地址 方便以后更改

#define BaseURL [NSURL URLWithString:@"http://localhost/"]

使用GET请求的JSON

-(void)GetJson{
    [[Manager shareManager]GET:@"demo,json" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败%@",error);
    }];
}

用POST请求登录账号

-(void)postLoad{
    NSDictionary *params = @{@"username":@"admin",@"password":@"admin"};
    [[Manager shareManager]POST:@"login.php" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败%@",error);
    }];
}

也用POST来做上传

-(void)upLoadFile{
    //拼上传的路径
    NSURL *imageURL = [[NSBundle mainBundle]URLForResource:@"1.jpg" withExtension:nil];
    NSURL *imageURL1 = [[NSBundle mainBundle]URLForResource:@"2.png" withExtension:nil];
    
    [[Manager shareManager]POST:@"upload-m.php" parameters:@{@"username":@"zhangsan"} constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        //请求的数据
        [formData appendPartWithFileData:[NSData dataWithContentsOfURL:imageURL] name:@"userfile[]" fileName:@"1.jpg" mimeType:@"image/jpg"];
        [formData appendPartWithFileData:[NSData dataWithContentsOfURL:imageURL1] name:@"userfile[]" fileName:@"2.png" mimeType:@"image/jpg"];
      //显示进度
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"%f",(CGFloat)uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败%@",error);
    }];
}

用downloadTask进行下载

-(void)downLoad{
    NSURL *url = [NSURL URLWithString:@"http://localhost/av.mp4"];
    //第一参数 要请求的路径
    [[[Manager shareManager]downloadTaskWithRequest:[NSURLRequest requestWithURL:url] progress:^(NSProgress * _Nonnull downloadProgress) {
        //显示下载进度
        NSLog(@"%f--%@",(CGFloat)downloadProgress.completedUnitCount / downloadProgress.totalUnitCount,[NSThread currentThread]);
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        NSLog(@"%@",targetPath);
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:response.suggestedFilename];
        return [NSURL fileURLWithPath:path];
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        NSLog(@"success %@",filePath.path);
    }]resume ];
}

常用的四个~~


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值