待机模式下的ios 5 NSURL连接和块下载
我正在使用待机模式下的ios 5 NSURL连接和块下载,ios,download,nsurlconnection,standby,dispatch-async,Ios,Download,Nsurlconnection,Standby,Dispatch Async,我正在使用NSURLConnection从服务器下载内容(我正在使用iOS 5.0开发iPad应用程序)。 我希望即使iPad处于待机状态,NSURLConnection也能继续下载。 可能吗 这是我的代码: -(void)startDownload { UIDevice* device = [UIDevice currentDevice]; BOOL backgroundSupported = NO; if ([device respondsToSelector:@s
NSURLConnection
从服务器下载内容(我正在使用iOS 5.0开发iPad应用程序)。
我希望即使iPad处于待机状态,NSURLConnection也能继续下载。
可能吗
这是我的代码:
-(void)startDownload {
UIDevice* device = [UIDevice currentDevice];
BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)])
backgroundSupported = device.multitaskingSupported;
NSLog(@"\n\nbackgroundSupported= %d\n\n",backgroundSupported);
dispatch_async(dispatch_get_main_queue(), ^ {
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:imageURL];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:NO];
[conn scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[conn start];
if (conn) {
NSMutableData *data = [[NSMutableData alloc] init];
self.receivedData = data;
}
else { ... }
}) ;
}
谢谢 每个应用程序在终止前都可以在后台继续执行大约10分钟。只有某些应用程序可以在后台继续执行,如音频/gps/蓝牙等相关应用程序。您可以在(左侧应用程序状态和多任务部分下)找到更多信息 下面的代码示例来自应用程序文档,可以帮助您开始,这样您的连接可以持续大约10分钟-
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
谢谢你的回复。不幸的是,当应用程序进入后台时我没有问题,但当iPad进入待机模式时我有问题哦,我明白了。“待机模式”到底是什么意思?我认为待机模式意味着应用程序进入后台。