-1

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 suppose user sends it to background mode(by clicking on home button) then how do I assure the download to continue?

  2. If the user force quits the app(doubleclick on home button and swipe the app from app switcher), then how do I ensure the app downloads the images?

I've been reading about background stuffs a lot. Few also say that in 2. case the download cannot continue.

Below are the links I referred:

http://www.appcoda.com/ios7-background-fetch-programming/ http://www.appcoda.com/background-transfer-service-ios7/ iOS Background downloads when the app is not active

I'm not getting the right approach to download the images in foreground, background/suspended, user force quits the app. I'm using AFNetworking for webservice calls.

Below is my code:

I've image details like the URL in .json file uploaded to amazon. To download the file I do,

[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://someurl/imageFile.json"]];

From this file I read the urls of images and download them along with image details. This is a big process. How Do I handle it? Please help..

Community
  • 1
  • 1
Medha
  • 39
  • 1
  • 6

1 Answers1

2

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.

Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
  • Thanks a lot.. I will try this.. But what about the second case? In iOS is i possible to download stuffs even if user quits the app from app switcher? – Medha Jul 05 '16 at 05:31
  • Ya.. I had read this in Apple doc.. But still even if the user quits the app, in android downloads do happen. I dont know why this feature is not there in iOS – Medha Jul 05 '16 at 05:46
  • Then should I download content when remote notifications are sent? Because If user quits the app, this delegate will always be called.. Is it the right way of doing? – Medha Jul 05 '16 at 05:49
  • @Medha If you setup a background NSURLSession task, the download is handled by the OS and not your app. Your app can terminate/crash whatever, but the download will not stop. – Ekta Padaliya Jul 05 '16 at 05:55
  • I tried the code and by browsing other sites regarding NSURLSession. It is working. But I have another question. If the app is in background say for 2 days.. If I want to download something after a day how should I do it? I mean how do I trigger the NSURLSession resume code again? – Medha Jul 05 '16 at 11:13
  • @Medha You have to go into the app background and resume it again. – Ekta Padaliya Jul 05 '16 at 11:23