0

我需要在应用程序处于空闲状态时将一些大图像下载到应用程序。我计划使用 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();
}
4

1 回答 1

0

NSURLSession API 可从 ios7 获得。

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

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

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

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

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

于 2014-06-12T10:24:01.910 回答