3

In My App I download the audio files from the server, And the files are downloaded fine when the app is in foreground and when I clicked home button or lock button to force the app to go to background, then after some time, the download is stopped and the error comes as the 1005 network connection lost. Whats the problem? Can Anybody explain the issue?

Code:

     NSURL *url = [NSURL URLWithString:currentURL];
            NSURLRequest *theRequest = [NSURLRequest requestWithURL:url         cachePolicy:NSURLRequestReloadIgnoringLocalCacheData  timeoutInterval:60];
            receivedData = [[NSMutableData alloc] initWithLength:0];
            NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self     startImmediately:YES];
            myConnection = connection;            
            NSLog(@"%@ Download Started", currentURL);


- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [receivedData setLength:0];
    expectedBytes = [response expectedContentLength];
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
    float progressive = (float)[receivedData length] / (float)expectedBytes;
    [downloadProgressView setProgress:progressive];
    NSInteger val = progressive*100;
    downloadpercentageLabel.text = [NSString stringWithFormat:@"%ld%@",(long)val,@"%"];       
    //[UIApplication sharedApplication].idleTimerDisabled = YES;       
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;   
}
AMI amitekh
  • 198
  • 3
  • 16
  • 1
    are you check the background fetch in background mode? – balkaran singh Jul 07 '16 at 10:50
  • 1
    This might be a duplicate. Have you checked out this question: http://stackoverflow.com/questions/8861390/ios-background-downloads-when-the-app-is-not-active ? – Mehmet Efe Akça Jul 07 '16 at 10:52
  • @MehmetEfeAkça I've tried several answers but didn't work out, And the accepted answer is not found. Please Help – AMI amitekh Jul 07 '16 at 10:53
  • @balkaransingh I don't know what is background fetch – AMI amitekh Jul 07 '16 at 10:55
  • 1
    Select your project -> capabilites -> Backgorund modes -> backgorund Fetch – balkaran singh Jul 07 '16 at 10:57
  • @balkaransingh Now checked it – AMI amitekh Jul 07 '16 at 11:05
  • UIBackgroundTaskIdentifier bgTaskId =[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [[UIApplication sharedApplication] endBackgroundTask:bgTaskId]; NSLog(@"%f",[UIApplication sharedApplication].backgroundTimeRemaining); }]; NSLog(@"%f",[UIApplication sharedApplication].backgroundTimeRemaining); – balkaran singh Jul 07 '16 at 11:08
  • add this code in applicationDidEnterBackground in app delegate and check. – balkaran singh Jul 07 '16 at 11:09
  • @balkaransingh - Don't confuse background `NSURLSession` with background fetch. They're completely different things. He wants background `NSURLSession`, not background fetch. And the `UIBAckgroundTaskIdentifier` approach is only good for an extra 3 minutes, but doesn't handle network interruptions gracefully. – Rob Jul 07 '16 at 11:17
  • @Rob what is that network interruptions? – AMI amitekh Jul 07 '16 at 11:25
  • then @Rob What should we use for log running task in background more than 3 minutes? can you guide me. – balkaran singh Jul 07 '16 at 11:26
  • See http://stackoverflow.com/a/20808917/1271826 – Rob Jul 07 '16 at 11:34
  • Please look at this answer. http://stackoverflow.com/questions/38195497/how-to-keep-downloading-new-images-in-background-even-if-user-force-quits-the-ap/38195715#38195715 – Ekta Padaliya Jul 07 '16 at 13:21
  • 1
    How to download multiple files one after other – AMI amitekh Jul 07 '16 at 14:13

1 Answers1

6

Use background NSURLSession. It handles network interruptions and downloads exceeding 3 minutes. See Downloading Content in the Background section of The App Programming Guide for iOS, which describes background downloads. Also refer to WWDC 2013 video in What’s New in Foundation Networking (it's covered later in the video).

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks Rob, That works fine for single url download, If I give the second URL its not working its failed to download – AMI amitekh Jul 07 '16 at 14:12
  • 2
    It works fine for multiple downloads, too. Just make sure to use the same session object (or, worse, instantiate a new one with a new identifier). – Rob Jul 07 '16 at 19:02