iOS Background downloads when the app is not active

后端 未结 8 1473
日久生厌
日久生厌 2020-11-27 13:28

When my app is initially downloaded, the user needs to download a large file something like 200MB (upper limit). I definitely can\'t expect the user to keep the app open til

8条回答
  •  無奈伤痛
    2020-11-27 13:30

    Add below in your - (void)applicationDidEnterBackground:(UIApplication *)application

    UIApplication  *app = [UIApplication sharedApplication];
    UIBackgroundTaskIdentifier bgTask;
    
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
            [app endBackgroundTask:bgTask]; 
    }];
    

    and you are good to go... I have this in one of my published download manager app

    This will work just fine. You can also check how much time you have, since apple only enable 10 minutes background tasks. Use:

    NSTimeInterval ti = [[UIApplication sharedApplication]backgroundTimeRemaining];
    NSLog(@"backgroundTimeRemaining: %f", ti); // just for debug
    

提交回复
热议问题