使用NSURLSession iOS在应用程序处于空闲状态时下载图像

时间:2014-06-12 05:54:43

标签: ios nsurlsession nsurlsessiondownloadtask

我需要在应用程序处于空闲状态时将一些大图像下载到应用程序。我正在计划使用NSURLSession.Tutorials完成它并且可用的示例代码工作并确认可以进行后台下载。这是我的要求的最佳方法吗?如果在下载未完成时从后台移除应用程序并且只获得了几个字节会发生什么情况。我可以从停止的地方恢复下载吗?我可以在iOS6中使用它?这些是我正在使用的委托方法。

- (NSURLSession *)backgroundSession
{
/*
 Using disptach_once here ensures that multiple background sessions with the same identifier are not created in this instance of the application. If you want to support multiple background sessions within a single process, you should create each session with its own identifier.
 */
    static NSURLSession *session = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.example.apple-samplecode.SimpleBackgroundTransfer.BackgroundSession"];
        session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    });
    return session;
}


- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
    BLog();

    /*
     Report progress on the task.
     If you created more than one task, you might keep references to them and report on them individually.
     */

    if (downloadTask == self.downloadTask)
    {
        double progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
        BLog(@"DownloadTask: %@ progress: %lf", downloadTask, progress);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.progressView.progress = progress;
        });
    }
}


- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)downloadURL
{
    BLog();

    /*
     The download completed, you need to copy the file at targetPath before the end of this block.
     As an example, copy the file to the Documents directory of your app.
    */
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
    NSURL *documentsDirectory = [URLs objectAtIndex:0];

    NSURL *originalURL = [[downloadTask originalRequest] URL];
    NSURL *destinationURL = [documentsDirectory URLByAppendingPathComponent:[originalURL lastPathComponent]];
    NSError *errorCopy;

    // For the purposes of testing, remove any esisting file at the destination.
    [fileManager removeItemAtURL:destinationURL error:NULL];
    BOOL success = [fileManager copyItemAtURL:downloadURL toURL:destinationURL error:&errorCopy];

    if (success)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImage *image = [UIImage imageWithContentsOfFile:[destinationURL path]];
            self.imageView.image = image;
            self.imageView.hidden = NO;
            self.progressView.hidden = YES;
        });
    }
    else
    {
        /*
         In the general case, what you might do in the event of failure depends on the error and the specifics of your application.
         */
        BLog(@"Error during the copy: %@", [errorCopy localizedDescription]);
    }
}


- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    BLog();

    if (error == nil)
    {
        NSLog(@"Task: %@ completed successfully", task);
    }
    else
    {
        NSLog(@"Task: %@ completed with error: %@", task, [error localizedDescription]);
    }

    double progress = (double)task.countOfBytesReceived / (double)task.countOfBytesExpectedToReceive;
    dispatch_async(dispatch_get_main_queue(), ^{
        self.progressView.progress = progress;
    });

    self.downloadTask = nil;
}


/*
 If an application has received an -application:handleEventsForBackgroundURLSession:completionHandler: message, the session delegate will receive this message to indicate that all messages previously enqueued for this session have been delivered. At this time it is safe to invoke the previously stored completion handler, or to begin any internal updates that will result in invoking the completion handler.
 */
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
{
    APLAppDelegate *appDelegate = (APLAppDelegate *)[[UIApplication sharedApplication] delegate];
    if (appDelegate.backgroundSessionCompletionHandler) {
        void (^completionHandler)() = appDelegate.backgroundSessionCompletionHandler;
        appDelegate.backgroundSessionCompletionHandler = nil;
        completionHandler();
    }

    NSLog(@"All tasks are finished");
}


-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
    BLog();
}

1 个答案:

答案 0 :(得分:0)

NSURLSession API可从ios7获得。

在NSURLSession中恢复数据 - 在某些情况下,您可以恢复已取消或在进行中失败的下载。为此,首先确保原始下载不会通过将NO传递给下载的setDeletesFileUponFailure:方法来删除其数据。如果原始下载失败,您可以使用resumeData方法获取其数据。然后,您可以使用initWithResumeData:delegate:path:方法初始化新下载。下载恢复后,下载代表将收到下载:willResumeWithResponse:fromByte:message。

只有当连接协议和正在下载的文件的MIME类型都支持恢复时,才能恢复下载。您可以使用canResumeDownloadDecodedWithEncodingMIMEType:method

确定您的文件的MIME类型是否受支持

如果您在后台会话中安排下载,则在您的应用未运行时,下载会继续。如果您在标准会话或短暂会话中安排下载,则必须在重新启动应用时重新开始下载。

在从服务器传输过程中,如果用户告诉您的应用暂停下载,您的应用可以通过调用cancelByProducingResumeData:方法取消该任务。稍后,您的应用程序可以将返回的恢复数据传递给downloadTaskWithResumeData:或downloadTaskWithResumeData:completionHandler:方法,以创建继续下载的新下载任务。

如果传输失败,则使用NSError对象调用您的代理的URLSession:task:didCompleteWithError:方法。如果任务可恢复,则该对象的userInfo字典包含NSURLSessionDownloadTaskResumeData键的值。您的应用应使用可访问性API来确定何时重试,然后应调用downloadTaskWithResumeData:或downloadTaskWithResumeData:completionHandler:创建新的下载任务以继续下载。