我有一个在iOS 9中不起作用的旧项目,我阅读了AFNetworking上的正式文档,并完成了大部分迁移。
NetworkManager:
_requestManager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:baseURL ]];
//here we can set the request header as the access token once we have logged in.
AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
[_requestManager setRequestSerializer:requestSerializer];
[_requestManager setResponseSerializer:responseSerializer];较早版本:
// 2. Create an `NSMutableURLRequest`.
NSMutableURLRequest *request =
[[NetworkManager sharedInstance].requestManager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:fullPath
parameters:nil
constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
NSString *fileName = [NSString stringWithFormat:@"%@.caf", theAudioItem.media_item_name];
[formData appendPartWithFileData:audioData name:theAudioItem.media_item_name fileName:fileName mimeType:@"audio/caf"];
}];
// 3. Create and use `AFHTTPRequestOperationManager` to create an `AFHTTPRequestOperation` from the `NSMutableURLRequest` that we just created.
AFHTTPRequestOperation *operation =
[[NetworkManager sharedInstance].requestManager HTTPRequestOperationWithRequest:request
success: ^(AFHTTPRequestOperation *operation, id responseObject) {
result(YES, @"");
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
if (operation.responseData) {
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:nil];
result(NO, [responseDict valueForKey:@"Message"]);
[self deleteTmpFilesFromParts:formParts];
} else {
result(NO, [NSString stringWithFormat:@"Failed to upload media to %@!", gallery.gallery_name]);
[self deleteTmpFilesFromParts:formParts];
}
result(NO, errorMessage);
}];
// 4. Set the progress block of the operation.
[operation setUploadProgressBlock: ^(NSUInteger __unused bytesWritten,
long long totalBytesWritten,
long long totalBytesExpectedToWrite) {
DLog(@"progress is %i %lld %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
progress((float)totalBytesWritten / (float)totalBytesExpectedToWrite);
}];
// 5. Begin!
[operation start];转换后的响应:(通过注释更新产生错误的内容)
//No visible @interface for 'AFHTTPRequestSerializer<AFURLRequestSerialization>' declares the selector 'multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:'
NSMutableURLRequest *request =
[[NetworkManager sharedInstance].requestManager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:fullPath parameters:nil
constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
NSString *fileName = [NSString stringWithFormat:@"%@.caf", theAudioItem.media_item_name];
[formData appendPartWithFileData:audioData name:theAudioItem.media_item_name fileName:fileName mimeType:@"audio/caf"];
}];
// 3. Create and use `AFHTTPRequestOperationManager` to create an `AFHTTPRequestOperation` from the `NSMutableURLRequest` that we just created.
//No visible @interface for 'AFHTTPSessionManager' declares the selector 'HTTPRequestOperationWithRequest:success:failure:'
NSURLSessionTask *operation =
[[NetworkManager sharedInstance].requestManager HTTPRequestOperationWithRequest:request
success: ^(NSURLSessionTask *operation, id responseObject) {
result(YES, @"");
} failure: ^(NSURLSessionTask *operation, NSError *error) {
//Error for operation doesn't have responseData
if (operation.responseData) {
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:nil];
result(NO, [responseDict valueForKey:@"Message"]);
[self deleteTmpFilesFromParts:formParts];
} else {
result(NO, [NSString stringWithFormat:@"Failed to upload media to %@!", gallery.gallery_name]);
[self deleteTmpFilesFromParts:formParts];
}
// get the response
result(NO, errorMessage);
}];
// 4. Set the progress block of the operation.
//No visible @interface for 'NSURLSessionTask' declares the selector 'setUploadProgressBlock:'
[operation setUploadProgressBlock: ^(NSUInteger __unused bytesWritten,
long long totalBytesWritten,
long long totalBytesExpectedToWrite) {
DLog(@"progress is %i %lld %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
progress((float)totalBytesWritten / (float)totalBytesExpectedToWrite);
}];
// 5. Begin!
//No visible @interface for 'NSURLSessionTask' declares the selector 'start'
[operation start];发布于 2016-02-11 10:29:24
AFHTTPSessionManager的等效代码是:
NSURLSessionTask *task = [[[NetworkManager sharedInstance] requestManager] POST:fullPath parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSString *fileName = [NSString stringWithFormat:@"%@.caf", theAudioItem.media_item_name];
[formData appendPartWithFileData:audioData name:theAudioItem.media_item_name fileName:fileName mimeType:@"audio/caf"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"%.3f", uploadProgress.fractionCompleted);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// success
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// failure
// if you need to process the `NSData` associated with this error (if any), you'd do:
NSData *data = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
if (data) { ... }
}];这包含了v1.x代码中的两个主要更改:第一,在2.x中引入了GET/POST/etc,这些方法帮助您避免手动启动操作(或将其添加到您自己的队列中)。其次,在3.x中,他们退出了NSOperation框架的AFHTTPRequestOperationManager,现在使用AFHTTPSessionManager类,其中GET/POST/etc。不要返回NSOperation子类,而是返回NSURLSessionTask引用。
https://stackoverflow.com/questions/35335804
复制相似问题