Answer the question
In order to leave comments, you need to log in
How to cheat using UIBackgroundTaskIdentifier?
Since I just started to delve into the intricacies of Objective-C and iOS frameworks, please do not throw stones if the question is stupid.
Problem - as far as I know, when minimizing the application, the system gives about 10 minutes of time to complete the tasks. But if you cheat and create a background task that starts a timer that will later destroy the current task and create a new one, you can win as much time as you like. But this only works during debugging, when the phone is connected to the poppy.
The question is - how to make this process work in the usual mode? :)
Here is the actual piece when, which does this:
- (void)startTask
{
if (self.bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}
self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"" expirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
}];
_timer = [NSTimer scheduledTimerWithTimeInterval:15.0
target:self
selector:Selector(startTask)
userInfo:nil
repeats:NO];
}
Answer the question
In order to leave comments, you need to log in
In general, there is one way. In iOS 6 it was possible to reset backgroundTimeRemaining by calling [locationManager startUpdatingLocation]. Now, unfortunately, this trick does not work. But! You can use audio sessions in the background and the system will not be able to kill your bgTask. Something like this :)
No need to cheat, applications should not constantly run in the background. This is only allowed if you intend to play music or stream video in the background, or use voip. You can also download something through NSURLSession for example. Read the guides, everything is detailed there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question