AFNetworking 3.0 后台上传不起作用

Posted

技术标签:

【中文标题】AFNetworking 3.0 后台上传不起作用【英文标题】:AFNetworking 3.0 Background Upload not working 【发布时间】:2016-06-06 11:59:38 【问题描述】:

我已经实现了

-(void)uploadMultipleImagesUsingAFNetworkingMultipartFormat:(id)sender 

NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[FileUtility basePath] stringByAppendingPathComponent:[self getPathOfRootImageFolderDocumentId:@"1"]] error:nil];

NSString *path = [directoryContents objectAtIndex:0];

NSString* filePath=[[[FileUtility basePath] stringByAppendingPathComponent:[self getPathOfRootImageFolderDocumentId:@"1"]] stringByAppendingPathComponent:path];

NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.uploadDocument.Background"];

MBProgressHUD *progres = [MBProgressHUD showHUDAddedTo:[[AppDelegate sharedInstance] window] animated:YES];
progres.mode = MBProgressHUDModeDeterminateHorizontalBar;
progres.progress = 0.0;

if (!self.sessionManager) 
    _sessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];


NSError *error;
NSMutableURLRequest *requet= [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"URLTO UPLOAD" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) 
    NSError *error1;
    [formData appendPartWithFileURL:[NSURL fileURLWithPath:filePath] name:@"fileUpload" fileName:path mimeType:@"image/jpeg" error:&error1];
    NSLog(@"%@",error1);


 error:&error];


NSURLSessionUploadTask *uploadTask = [self.sessionManager uploadTaskWithRequest:requet fromFile:[NSURL fileURLWithPath:filePath] progress:^(NSProgress * _Nonnull uploadProgress) 
    dispatch_async(dispatch_get_main_queue(), ^
        [progres setProgress:uploadProgress.fractionCompleted];
    );

    NSLog(@" Progress %f",uploadProgress.fractionCompleted);
 completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) 
    dispatch_async(dispatch_get_main_queue(), ^
        [MBProgressHUD hideHUDForView:[AppDelegate sharedInstance].window animated:YES];
    );

];
[uploadTask resume];



但是当我把它放在后台,并在一段时间后激活。我观察到该过程从暂停的地方开始。

如何在后台继续上传任务?

【问题讨论】:

参考这篇文章***.com/questions/7800614/… 您的问题解决了吗? 不,找不到任何解决方案@EktaMakadiya @MikeAlter 请检查我的回答。希望对您有所帮助。 @EktaMakadiya,谢谢你的回答,目前我没有做这个任务会检查并让你知道 【参考方案1】:

请在后台模式下进行后台获取,然后尝试 :)

【讨论】:

你的未来没有成功。进入后台模式时已取消下载【参考方案2】:

您只需要添加以下代码,因为后台任务的 NSURLSessionTask 中存在错误。

// Prepare a temporary file to store the multipart request prior to sending it to the server due to an alleged
// bug in NSURLSessionTask.
NSString* tmpFilename = [NSString stringWithFormat:@"%f", [NSDate timeIntervalSinceReferenceDate]];
NSURL* tmpFileUrl = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:tmpFilename]];


// Dump multipart request into the temporary file.
[[AFHTTPRequestSerializer serializer] requestWithMultipartFormRequest:requet
                                          writingStreamContentsToFile:tmpFileUrl
                                                    completionHandler:^(NSError *error)
 
     // Once the multipart form is serialized into a temporary file, we can initialize
     // the actual HTTP request using session manager.


     // Here note that we are submitting the initial multipart request. We are, however,
     // forcing the body stream to be read from the temporary file.

     NSURLSessionUploadTask *uploadTask = [self.sessionManager uploadTaskWithRequest:requet fromFile:tmpFileUrl progress:^(NSProgress * _Nonnull uploadProgress) 
         dispatch_async(dispatch_get_main_queue(), ^
             [progres setProgress:uploadProgress.fractionCompleted];
         );

         NSLog(@" Progress %f",uploadProgress.fractionCompleted);
      completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) 
         dispatch_async(dispatch_get_main_queue(), ^
             [MBProgressHUD hideHUDForView:[AppDelegate sharedInstance].window animated:YES];
         );

     ];

     [uploadTask resume];
 ];

希望这会对您有所帮助。如果您有任何问题,请提出。

【讨论】:

以上是关于AFNetworking 3.0 后台上传不起作用的主要内容,如果未能解决你的问题,请参考以下文章

使用AFnetworking以多部分格式上传图像在ios中不起作用

AFNetworking 3.0 下载后台模式

AFNetworking 3.0 上传图片

使用 AFNetworking 3.0 上传图像 [关闭]

无法使用 AFNetworking 3.0 上传图像

从 imagePickerController AFNetWorking 3.0 上传图像到服务器