iOS Running a timer within your app is in the background
Sometimes, you need to run the timer when the application is in the background as you do this?
Simple solution is to use a function applicationDidEnterBackground, which can we found in AppDelegate.
This function starts automatically if you go by in the background.
In this function we can create UIBackgroundTaskIdentifier, which run task after time:
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
- (void)applicationDidEnterBackground:(UIApplication *)application { UIApplication *app = [UIApplication sharedApplication]; //create new uiBackgroundTask __block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; //and create new timer with async call: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //run function methodRunAfterBackground NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(methodRunAfterBackground) userInfo:nil repeats:NO]; [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode]; [[NSRunLoop currentRunLoop] run]; }); } |
In this case the function (methodRunAfterBackground) starts 10 seconds after you switch applications in the background
Posted on 9 August 2013
I cant wait to go through additional from you. That is really an incredible weblog.
In NSTimer i set 6o and repeat =yes ,But this will be work for only 10 minutes, after 10 min. timer is stopped,
as there any other solution for run app in background,
Look at this post: http://zaachi.com/2013/09/30/ios-locationmanager-location-update-in-my-own-interval-with-application-in-the-background.html
This is a solution where the task can run in the background for a long time
Applications must be set up to support the actions that run in the background. You can combine location update in interval and your method.
iOS does not support to run the method in background in loop pernamently. You must combine it with allowed services.
i used same method but after some time it will force to app shutdown and timer will stop
it give me “has active assertions beyond permitted time” error and force to shutdown app.
plz note repeat mode = yes
Very nice tutorial its help me lots.
Hi I used the below code for making calls and sending messages but the thing is when i run on device in background messages are not going again when I open the app from back ground it working agian
__block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(startTrackingBg) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
});
Thanks a lot, was searching for this. Subscribing you
Thanks. It was really a helpful tutorial.