Hi,
I have an application which is running properly in the background before 10.3.3.
Since iOS 10.3.3, whenever the app goes to background AND the screen is turned off AND it is not hooked up with the charging cable, the background task is stopped running.
I started the background task when the app goes to background and do some background handling by using:
if (self.taskId == UIBackgroundTaskInvalid)
{
_isSuspended = NO;
UIApplication *app = [UIApplication sharedApplication];
self.taskId = [app beginBackgroundTaskWithExpirationHandler:^
{
[self stopAllTimer];
[app endBackgroundTask:self.taskId];
self.taskId = UIBackgroundTaskInvalid;
_isSuspended = YES;
}];
}
I have put a log to check how much time is left for the background task like this:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Background task started");
while (true) {
NSLog(@"background time remaining: %8.2f", [UIApplication sharedApplication].backgroundTimeRemaining);
[NSThread sleepForTimeInterval:1];
}
});
If I run the app in the backgrpound in a physical phone with screen on or if the phone is hooked up with a charging cable, it is working fine and I can see the background time remaining log statement every second for the whole 180s background time period.
But as long as when the phone is not hooked up with the charging cable and the screen is locked, I can't see the background time remaining log statement. As the phone is not hooked up with the cable, I am using the Profile and Log (https://developer.apple.com/bug-reporting/profiles-and-logs/?platform=ios) to check the sysdiagnose log. And the log is like:
2017-10-24 15:46:49.078108 xxxxxxxxx background time remaining: 177.95
.....
2017-10-24 15:46:50.289287 assertiond System will sleep.
.....
2017-10-24 15:46:50.293813 xxxxxxxxx background time remaining: 176.74
2017-10-24 15:47:47.386712 locationd system will power on
2017-10-24 15:47:47.386716 locationd system exiting sleep
........
2017-10-24 15:47:47.636843 xxxxxxxxx background time remaining: 175.64
I run the app and put it to background and leave it running for a while and then turn off the screen at 15:46:50. And from the log, you can tell the background task is stopped since 15:46:50.293813. It is because I have got a local notification and it turns on the screen so it "wakes" up the background task at 15:47:47.636843.
From the log, it seems like iOS has stopped the background task running when the screen is locked and resume the task when the screen is on again.
As my app is required to track a location at every minute in the background, this behaviour cause my app not working since 10.3.3.
Does anyone of you know how to solve this issue?
Thanks,
Kenneth