iOS网络之06AFN的文件上传与网络监听

AFN的上传:
  • 这次用的POST方法跟刚才的有所不同:
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];

    NSString *str = @"你的地址";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"username"] = @"zhangdanfeng";
    NSData *data = UIImageJPEGRepresentation(self.imageView.image, 1);

    [mgr POST:str parameters:params constructingBodyWithBlock:^(id  _Nonnull formData) {

        [formData appendPartWithFileData:data name:@"file" fileName:@"zhang.jpg" mimeType:@"image/jpeg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
网络监听
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];

    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"WIFI");
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"3G");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"NO NETWORK");
                break;
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"WEIZHI");
                break;
            default:
                break;
        }
    }];

    [mgr startMonitoring];

你可能感兴趣的:(iOS网络之06AFN的文件上传与网络监听)