- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的应用需要从用户手机上传视频文件,然后在服务器上进行处理。问题是文件的大小可以超过 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/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!