我正在尝试将曾经拥有 AFNetworking 2.6.3 的项目升级到 AFNetworking 3.1。
我有一个 URL 请求:
NSURL *requestURL = [NSURL URLWithString:url];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"PUT"];
[request setValue:mimeType forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[data length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"public-read" forHTTPHeaderField:@"x-amz-acl"];
[request setURL:requestURL];
这是旧版本的代码:
__block AFHTTPRequestOperation *uploadOperation;
uploadOperation = [[AFHTTPRequestOperationManager manager] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
//something here (gets called if successful)
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//something here (gets called if failed)
}] ;
[uploadOperation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
//this gets repeatedly called where I could calculate percentage
}];
[uploadOperations addObject:uploadOperation];
我没有触及 URL 或 URL 请求。我正在尝试将此调用转换为兼容 AFNetworking 3.1 的调用。
这是我的新代码:
__block NSURLSessionUploadTask *uploadOperation;
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//manager.responseSerializer = [AFJSONResponseSerializer serializer];
uploadOperation = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
//this should be called on upload
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if(error){
//this should be called in case of failure
}else{
//this should be called if file gets uploaded successfully.
}
}];
[uploadOperations addObject:uploadOperation];
但是,进度块和成功/失败块都不会被调用。绝不。我已经仔细检查了所有内容都不是nil
,否则一切看起来都很完美。
我究竟做错了什么?