我正在使用Afnetworking和我的项目,我想在后台下载大约100mb或150mb的大文件,但是在苹果文档中他们说后台任务将持续10分钟,所以我该如何解决这个问题?
我在互联网上搜索我idle timer禁用它或根据文件设置计时器?(但我如何根据我的文件大小设置空闲计时器,它还取决于互联网连接)..
在某些SO帖子中,我发布了这个答案,但我不确定他是否会为此afnetworking background answer
禁用闲置音色(void)applicationWillResignActive:(UIApplication *)application {
__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
[application endBackgroundTask:backgroundTaskIdentifier];
[[YourRestClient sharedClient] cancelAllHTTPOperations];
}];
这是我的afnetworking代码
NSURL *url = [NSURL URLWithString:downloadMainURL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient.operationQueue setMaxConcurrentOperationCount:1];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:downloadPostUrl parameters:postRequest];
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:destPath shouldResume:YES];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if(operation.response.statusCode == 200) {
NSLog(@"enable ipad sleep");
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];// enable the idle screen timmer
[loadingHUD hide:TRUE];
// NSLog(@"responseString is %@",[operation responseString]);
}
else{
NSLog(@"setCompletionBlockWithSuccess");
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];// enable the idle screen timmer
[loadingHUD hide:TRUE];
}
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if(operation.response.statusCode!= 200) {
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];// enable the idle screen timmer
[loadingHUD hide:TRUE];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Attention" message:@"download failed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}];
[httpClient enqueueHTTPRequestOperation:operation];
[operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
{
int filesize = [[NSUserDefaults standardUserDefaults] integerForKey:@"filesize"];
int totalbytes=filesize+bytesWritten;
NSLog(@"productidFileSize value is %f",productidFileSize);
NSLog(@"totalBytesWritten value is %d",totalbytes);
progress = ((float)totalbytes )/ productidFileSize;
NSLog(@"progress value is %f",progress);
loadingHUD.progress = progress;
[[NSUserDefaults standardUserDefaults] setInteger:totalbytes forKey:@"filesize"];
}];
}
}
答案 0 :(得分:0)
没有办法“让iOS延长10米的限制”。
我相信你有两个选择:
除此之外,您需要使用加载程序在UI中执行此操作,并要求您的用户免费下载。
答案 1 :(得分:0)