Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 Background transfer service stops after 3 minutes

I have created a sample code to download a file from network repeatedly(every 30 secs or so). In iOS 7 using Background transfer services with NSURLSession
I followed this tutorial http://mobile.tutsplus.com/tutorials/iphone/ios-7-sdk-background-transfer-service/ and added a timer like this to repeat it.

    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
mute = [NSTimer scheduledTimerWithTimeInterval:30.0f
                                        target:self
                                      selector:@selector(startDownload)
                                      userInfo:nil
                                       repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:mute forMode:NSRunLoopCommonModes];

When I run it(in background by clicking home button) in simulator and on iPad connected to the Xcode(where I can see the logs) everything works fine and it keeps downloading continuously. But when I disconnect the iPad from the Mac and run it on the iPad in background after around 3 seconds it stops running (handleEventsForBackgroundURLSession in AppDelegate get called).

In Xcode project capabilities I have selected Background fetch as Background Modes.

What am I missing here or what have I done wrong so that it gets stop after around 3 minutes? (According to the documentations with iOS 7 Background transfer services it should run continuously as there's no time limit in background for this.)

Thanks

like image 687
Madhu Avatar asked Dec 26 '22 16:12

Madhu


2 Answers

Background tasks in iOS7 will only give you 30 seconds at most (dramatically down from 10 minutes). You should use the new fetch background mode instead. You should not be using a timer, but use the newly provided API to ask the OS to be woken up in regular intervals and set up your download using NSURLSession.

like image 89
Léo Natan Avatar answered Dec 28 '22 08:12

Léo Natan


Background Fetch is something different. Background Fetch will wake up your app for periodic fetches of new data (typically, a < 30s network request looking for updates). This is not related to background NSURLSessions and should probably be turned off if you aren't actually using it for queuing NSURLSessionDownloadTasks or other update purposes.

From what I understand, it's possible that the behavior you are seeing is actually normal. I do not believe that background NSURLSessionDownloadTasks are guaranteed to run continuously or on any device configuration (AC vs battery, WiFi vs Cellular, etc.) You said that you disconnected from your Mac which would switch device state from charging to battery. Among other factors, that could be enough to pause transfers or decrease download limits. Unfortunately, this system appears to be very opaque to developers.

To be sure you are getting the highest priority available, make sure your discretionary property on your NSURLSessionConfiguration is set to false. Download tasks created while the app is in the background will always have this set to true, so just an FYI there.

Apple's sample code will put you on the right track: https://developer.apple.com/library/iOS/samplecode/SimpleBackgroundTransfer/Introduction/Intro.html

like image 23
Nate T Avatar answered Dec 28 '22 09:12

Nate T