我正在使用要在后台下载数据的应用程序。根据 Apple 多任务处理指南,您可以下载 10 分钟的数据。
就我而言,下载文件需要 10 多分钟,然后下载失败。
初始下载请求来自 DownloadViewController,如下所示。
- (IBAction)performLargeUpload:(id)sender {
[request cancel];
[self setRequest:[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://mirrorblender.top-ix.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_stereo.avi"]]]; // 149MB
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"big_buck_bunny_480p_stereo.avi"]];
[request setTimeOutSeconds:20];
[request setDownloadProgressDelegate:progressIndicator];
[request setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
[request setShouldContinueWhenAppEntersBackground:YES];
#endif
[request setDelegate:self];
[request setDidFailSelector:@selector(uploadFailed:)];
[request setDidFinishSelector:@selector(uploadFinished:)];
[request setAllowResumeForFileDownloads:YES];
[request startAsynchronous];
[resultView setText:@"Downloading data..."];
}
在 appDelegate 中,我在 applicationDidEnterBackground 时放置了这段代码
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
backgroundTimer=nil;
backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(keepAlive) userInfo:nil repeats:YES];
}
如何延长后台下载时间??