Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTimer does't work when device lock

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.

like image 414
Sina Avatar asked Apr 08 '26 00:04

Sina


1 Answers

To run code in background refer this link :- https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

Notes :-

  1. Apps only get 10 mins of background execution - after this time duration the timer will stop firing.
  2. As of iOS 7 or higher when the device is locked it will suspend the foreground app almost instantly. The timer will not fire after an iOS 7 or higher app is locked.
like image 129
Darshan Mothreja Avatar answered Apr 09 '26 12:04

Darshan Mothreja