ios - 如何在 ios 中处理大文件上传?

标签 ios iphone afnetworking afnetworking-2

我的应用需要从用户手机上传视频文件,然后在服务器上进行处理。 问题是文件的大小可以超过 200 MB,用户不会保持应用程序打开以等待文件上传。由于苹果不允许应用程序在后台运行超过有限的时间。我怎样才能确保我的文件被上传。我正在使用 afnetworking 设置 ios 7 库给出的上传任务。

如果有人能指出正确的方向或有任何解决方案,我将不胜感激。我已经为此苦苦思索了太久。谢谢。

        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];


    [manager setTaskDidSendBodyDataBlock:^(NSURLSession *session,NSURLSessionTask *task ,int64_t bytesSent, int64_t totalBytesSent,int64_t totalBytesExpectedToSend){
        CGFloat progress = ((CGFloat)totalBytesSent / (CGFloat)sensize);

       NSLog(@"Uploading files %lld  -- > %lld",totalBytesSent,totalBytesExpectedToSend);
        [self.delegate showingProgress:progress forIndex:ind];
    }];



    dataTask = [manager uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {

        }

       }];

我的请求是一个普通的多部分表单请求。

最佳答案

使用:

NSURLSessionConfiguration:backgroundSessionConfiguration:

代替

NSURLSessionConfiguration:defaultSessionConfiguration

来自NSURLSessionConfiguration:backgroundSessionConfiguration: documentation :

Upload and download tasks in background sessions are performed by an external daemon instead of by the app itself. As a result, the transfers continue in the background even if the app is suspended, exits, or crashes.

所以在你的情况下,改变:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

到:

NSString *appID = [[NSBundle mainBundle] bundleIdentifier];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:appID];

在您的应用程序代理上实现 application:handleEventsForBackgroundURLSession:completionHandler: 将允许您的应用程序在上传完成时被唤醒(即在后台模式下取消挂起或取消终止)(无论是它是否已成功完成)。

不要与后台抓取混淆。你不需要它。后台获取只是唤醒您的应用程序,定期让您的应用程序有机会定期获取少量内容。但是,它对于定期重新启动失败的“后台模式”上传可能很有用。

关于ios - 如何在 ios 中处理大文件上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19343053/

相关文章:

php - AFNetworking PostPath php 参数为空

ios - AFNetworking 中的并发任务

iphone - 删除txt文件iOS中的一行文本

ios - swift : Sort array by natural order

iOS 应用内购买非续订订阅收据验证

ios - 如何在快速单击按钮时获取 tableview 标签值

iphone - 为 iPad 和 iPhone 设计输入按钮

ios - 将动态文本字段单元格插入 TableView 中的部分 - Swift

ios - 如何保护来自代理的api调用

android - 移动应用程序原型(prototype)制作工具