2017-02-13 74 views
10

我正在尝试使用NSURL下载一些文件,并使用nsurlsessiontask进行后台会话。当应用程序在调试模式下运行(当设备连接到Xcode时)时,一切都很有用,当从Xcode拔下设备(iPad)时不起作用。NSURLSession后台下载无法正常工作

我在iOS 9.3.5中使用Xcode 7.3.1。我已经花了数周来追踪这种奇怪的行为,但没有取得任何突破。可能是我错过了一些实现后台下载。 最近升级了Xcode到8.1.2和iOS到10.2.1,假设升级可能会解决问题,但事实并非如此。

+5

更新您与您的代码问题,所以很容易找到一些bug,或者你面对任何类型的错误 –

+2

添加你的代码有助于理解你在做什么错误? –

+2

您应该使用NSURLSessionDownloadTask而不是NSURLSessionTask来下载文件。并且请显示代码。否则,这只是一个猜测游戏,永远不会结束。 – GeneCode

回答

0

结帐我的工作代码,

NSURL *url = [NSURL URLWithString:imageURL]; 
NSURLSessionTask *_imageDownloadTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
    if (data) { 
      //Here you can read your files from data 
     if (image) { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
        //Save your files here for cache purpose 
       @try { 
        //You can handle onDownloadFinishWithFile: here too using delegate protocol 
       } 
       @catch (NSException *exception) { 
          NSLog(@"%@", exception.reason); 
       } 
       @finally { 
        // Defines a block of related code that is subsequently executed whether an exception is thrown or not. 
       } 
      }); 
     } 
    } 
}]; 
[_imageDownloadTask resume]; 

[注:我使用上面的代码图像下载]。

0

确保在功能背景模式下的背景获取被检查。 enter image description here

+0

已启用此选项 –

1

在项目导航器中,选择顶部的项目目标。接下来,在主窗口中点击Capabilities选项卡,所有可以使用开关按钮启用或禁用的功能将显示在那里。其中,找到背景模式区域(从结尾开始),然后单击其右侧的开关按钮以启用它。 enter image description here

然后swith'后台提取'。

enter image description here

+0

已添加此选项 –

0

参考以下链接,然后按照步骤

https://www.appcoda.com/background-transfer-service-ios7/

-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{ 
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate; 

// Check if all download tasks have been finished. 
[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) { 
    if ([downloadTasks count] == 0) { 
     if (appDelegate.backgroundTransferCompletionHandler != nil) { 
      // Copy locally the completion handler. 
      void(^completionHandler)() = appDelegate.backgroundTransferCompletionHandler; 

      // Make nil the backgroundTransferCompletionHandler. 
      appDelegate.backgroundTransferCompletionHandler = nil; 

      [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
       // Call the completion handler to tell the system that there are no other background transfers. 
       completionHandler(); 

       // Show a local notification when all downloads are over. 
       UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
       localNotification.alertBody = @"All files have been downloaded!"; 
       [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; 
      }]; 
     } 
    } 
}]; 

}

+0

欢迎您提供解决方案的链接,但请确保您的答案在没有它的情况下有用:[在链接附近添加上下文](// meta.stackexchange.com/a/8259)所以你的同行用户会有一些想法是什么,为什么它在那里,然后引用你链接到的页面最相关的部分,以防目标页面不可用。 [仅仅是一个链接的答案可能会被删除。](// stackoverflow.com/help/deleted-answers) – FelixSFD