【问题标题】:ios 5 NSURLConnection and block download in standby modeios 5 NSURLConnection 并在待机模式下阻止下载
【发布时间】:2012-11-09 09:57:02
【问题描述】:

我正在使用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 {  ... }
    }) ;

}

谢谢!

【问题讨论】:

    标签: ios download nsurlconnection standby dispatch-async


    【解决方案1】:

    每个应用都可以在后台继续执行大约 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;
        });
    }
    

    【讨论】:

    • 感谢您的回复。不幸的是,当应用程序进入后台时我没有问题,但是当 iPad 进入待机模式时我遇到了问题
    • 哦,我明白了。你到底是什么意思“待机模式”?我认为待机模式意味着应用程序进入后台。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多