T
T
Tony Stark2014-11-06 14:31:51
Objective-C
Tony Stark, 2014-11-06 14:31:51

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];
}

UPD. I also noticed this: when the phone is connected to the poppy, the backgroundTimeRemaining time = 0, but the application works: D and in normal mode it worked for me for 20 (!) minutes, and another time 4. The magic is straight :)

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
T
Tony Stark, 2014-11-10
@as_thunderbolt

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 :)

S
s0L, 2014-11-07
@s0L

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 question

Ask a Question

731 491 924 answers to any question