gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:31:45 25 4
gpt4 key购买 nike

我的应用需要从用户手机上传视频文件,然后在服务器上进行处理。问题是文件的大小可以超过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com