在downloadTask完成时从未调用handleEventsForBackgroundURLSession [英] handleEventsForBackgroundURLSession never called when a downloadTask finished

查看:537
本文介绍了在downloadTask完成时从未调用handleEventsForBackgroundURLSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AFURLSessionManager,并将管理器设置为单例实例.

I am using AFURLSessionManager, and set the manager as a singleton instance.

- (AFURLSessionManager *)backgroundSession
{
    static AFURLSessionManager *backgroundSession = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.testBackground.BackgroundDownload.BackgroundSession1234"];
    backgroundSession = [[AFURLSessionManager alloc]initWithSessionConfiguration:config];
    [backgroundSession setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite){
        NSLog(@"i am downloading my id = %d progress= %f",downloadTask.taskIdentifier, totalBytesWritten*1.0/totalBytesExpectedToWrite);
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    }];
    [backgroundSession setDownloadTaskDidFinishDownloadingBlock:^NSURL *(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location){
        NSLog(@"download finished");
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        return location;

    }];
    });
    return backgroundSession;
}
//assign a download task
NSURLSessionDownloadTask *task = [manager1 downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        return targetPath;
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    NSLog("%download success");
}];
[task resume];

我发现当我将应用程序切换到后台时,下载任务正在运行,但是完成后,系统调用handleEventsForBackgroundURLSession将永远不会被调用,我感觉我错过了一些设置或选项.任何想法对我都会有用,非常感谢.

I found that when I switch the app to the background the download task is running but when it was finished, the system call handleEventsForBackgroundURLSession will never be called.I am feeling that I have missed some setting or options. Any idea will be useful for me, thanks a lot.

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler;

推荐答案

如苹果公司所述:

如果iOS应用被系统终止并重新启动,则该应用可以使用相同的标识符来创建新的配置对象和会话,并检索终止时正在进行的传输状态.此行为仅适用于系统正常终止应用程序的情况.如果用户从多任务屏幕中终止该应用,则系统会取消所有会话的后台传输.此外,系统不会自动重新启动由用户强制退出的应用程序.用户必须明确重新启动该应用程序,然后才能再次开始传输.

If an iOS app is terminated by the system and relaunched, the app can use the same identifier to create a new configuration object and session and retrieve the status of transfers that were in progress at the time of termination. This behavior applies only for normal termination of the app by the system. If the user terminates the app from the multitasking screen, the system cancels all of the session’s background transfers. In addition, the system does not automatically relaunch apps that were force quit by the user. The user must explicitly relaunch the app before transfers can begin again.

希望它会有所帮助. 斯蒂芬

Hope it helps. Stefan

这篇关于在downloadTask完成时从未调用handleEventsForBackgroundURLSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆