gpt4 book ai didi

ios 5 NSURLConnection 和待机模式下的 block 下载

转载 作者:行者123 更新时间:2023-11-29 04:18:44 25 4
gpt4 key购买 nike

我正在使用 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/蓝牙等相关应用程序。您可以通过Background Execution and Multitasking了解更多信息(在左侧的“应用程序状态和多任务处理”部分下)。

以下代码示例来自应用文档,可以帮助您开始使用,以便您的连接可持续长达约 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;
});
}

关于ios 5 NSURLConnection 和待机模式下的 block 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13305780/

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