I have a app that i want work in background for every time and only when the user closed(terminated) it fails(like daily alarm). My code is:
AppDelegate:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
backgroundUpdateTask = 0;
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application{
}
- (void)applicationDidEnterBackground:(UIApplication *)application{
backgroundUpdateTask = [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
if (application.applicationIconBadgeNumber > 0) {
application.applicationIconBadgeNumber = 0;
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application{
[self endBackgroundUpdateTask];
if (application.applicationIconBadgeNumber > 0) {
application.applicationIconBadgeNumber = 0;
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
lte = [[NSUserDefaults standardUserDefaults] valueForKey:@"LTE"];
if (lte == nil) {
[Utility GetNewNotification:lte];
lte = [[[notifyDic objectForKey: @"Notification"]valueForKey:@"LTE"]valueForKey:@"text"];
[[NSUserDefaults standardUserDefaults] setValue:lte forKey:@"LTE"];
}
}
- (void)applicationWillTerminate:(UIApplication *)application{
}
- (void)endBackgroundUpdateTask{
[[UIApplication sharedApplication]endBackgroundTask:backgroundUpdateTask];
backgroundUpdateTask = UIBackgroundTaskInvalid;
}
and View Controler:
@implementation ViewController
{
NSTimer *myTimer;
int counter;
}
-(void)CheckTimer{
if(counter == 0){
counter = 60;
NSString* lte = [[NSUserDefaults standardUserDefaults] valueForKey:@"LTE"];
[Utility GetNewNotification:lte];
}
else {
counter --;
//do your video playing work here
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
/// Counter for get new notification
counter = 60;
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(CheckTimer) userInfo:nil repeats:YES];
}
I want every 60 second timer is trigger and service is call. but after 180 second after app go to background timer is down. and if device is lock timer is down too.
To run code in background refer this link :- https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
Notes :-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With