Migrate AFNetworking 2.x -> 3.x
Document
https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-3.0-Migration-Guide
Tips
simple request
2.x
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
[manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
onSuccess();
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
onFailed(error);
}];
3.x
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
onSuccess();
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
onFailed(error);
}];
Get status code in failure callback
2.x
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
onFailed([AppError readErrorResponse:operation error:error], [operation.response statusCode]);
}
3.x
NSHTTPURLResponse *response = error.userInfo[AFNetworkingOperationFailingURLResponseErrorKey];
response.statusCode;
"PUT" MultipartForm Request
2.x
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"PUT" URLString:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData image name:@"image" fileName:@"test.jpg" mimeType:@"image/jpeg"];
}
} error:nil];
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
[manager.operationQueue addOperation:operation];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"PUT" URLString:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (image) {
[formData image name:@"image" fileName:@"test.jpg" mimeType:@"image/jpeg"];
}
} error:nil];
[[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (!error) {
NSLog(@"success");
} else {
NSLog(@"Error: %@", error);
}
}] resume];