How to keep downloading new images in background even if user force quits the app in iOS objective C?

后端 未结 1 1963
滥情空心
滥情空心 2021-01-07 16:04

I want to download many images around 300, and display it in the UI.

I have two questions:

  1. If the app is in foreground, and suppos

相关标签:
1条回答
  • 2021-01-07 16:12

    Solution for your 1 question is below :

    Background sessions let you perform uploads and downloads of content in the background while your app is not running. You can create a background session configuration by calling the backgroundSessionConfiguration: method on the NSURLSessionConfiguration class.

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
    NSURLSessionConfiguration *sessionConfig;
    float timeout = 5 * 60.0f;
    
    BOOL iOS8OrNew = [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0;
    if (iOS8OrNew) {
        sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier];
    request.timeoutInterval = timeout;
    }
    else {
        sessionConfig = [NSURLSessionConfiguration backgroundSessionConfiguration:identifier];
    sessionConfig.timeoutIntervalForRequest = timeout;
    }
    
    sessionConfig.HTTPMaximumConnectionsPerHost = 10;
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:sessionConfig];
    
     NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request];
    
    
    [manager setDidFinishEventsForBackgroundURLSessionBlock:^(NSURLSession * _Nonnull session) {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        if (appDelegate.backgroundSessionCompletionHandler) {
            void (^completionHandler)() = appDelegate.backgroundSessionCompletionHandler;
            appDelegate.backgroundSessionCompletionHandler = nil;
            completionHandler();
        }
        NSLog(@"All tasks are finished");
    }];
    

    Add below code in your AppDelegate:

     - (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier
      completionHandler:(void (^)())completionHandler {
       self.backgroundSessionCompletionHandler = completionHandler;
    
       //add notification
       [self presentNotification];
    }
    
    -(void)presentNotification{
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.alertBody = @"Download Complete!";
        localNotification.alertAction = @"Background Transfer Download!";
    
        //On sound
        localNotification.soundName = UILocalNotificationDefaultSoundName;
    
        //increase the badge number of application plus 1
        localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
    
        [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
    }
    

    For your 2 solution

    If the system kills your app and your background session has active downloads, your downloads will continue and the system will launch your app when the downloads complete. However, if a user force quits your app, all tasks get cancelled.

    0 讨论(0)
提交回复
热议问题