autolocking ios device, NSTimer and scheduledTimerWithTimeInterval

897 views Asked by At

I would like to trigger action after some ammount of time (in production that will be 30 minutes) and right now I'm using NSTimers scheduledTimerWithTimeInterval. During tests (with timeout being 20 seconds, not 1800 seconds) everything seems fine. In debbuging mode (running from XCode) everything seems fine as well, because the device does not autolock. But in real life, when application is ran on device, autolocking (precisely autolocking, triggering lock button does not) "freezes" the timer (or at least moves the timer trigger somehow to future).

Can I handle that situation? Of course I can disable idleTimer in UIApplication sharedApplication but when application will enter background mode ipad still can autolock.

2

There are 2 answers

0
rordulu On BEST ANSWER

Actually you can,

First of all, create a default value for "startTimeOfMyExoticTimer" to NSUserDefaults:

[[[NSUserDefaults] standardDefaults] setObject:[NSDate date] forKey:@"startTimeOfMyExoticTimer"];

Then kick of a timer to check if the valid time range is over:

[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(checkIfTimeIsOver) userInfo:nil repeats:YES];

Then implement your checker method:

-(void)checkIfTimeIsOver{
     NSDate * now = [NSDate date];
     NSDate * startTime = [[NSUserDefaults standardDefaults] objectForKey:@"startTimeOfMyExoticTimer"];

     // Here compare your now and startTime objects. (subsract them) and see the difference between them. If your time range is to be set on 30 seconds. Then you should check if the time difference between those objects are bigger than 30 seconds.

}

This will be working even if the device is locked, the app is in background etc.

This approach worked for me, hope it'll work for you too

0
Nguyen Tran On
//Implement this block help your application run background about 10 minutes    

@interface AppDeledate(){
     UIBackgroundTaskIdentifier backgroundTaskId;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // if missing interval exist > start timer again and set missing interval to 0
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
        NSLog(@"Did Enter Background");

        backgroundTaskId = [application beginBackgroundTaskWithExpirationHandler:^{
            NSLog(@"Background Task Expired !!!\n");

            [application endBackgroundTask:backgroundTaskId];
            backgroundTaskId = UIBackgroundTaskInvalid;
                    //save missing interval to NSUserDefault
        }];
    }

    - (void)applicationWillEnterForeground:(UIApplication *)application
    {
        NSLog(@"Will Enter Foreground");

        if (backgroundTaskId && backgroundTaskId != UIBackgroundTaskInvalid) {
            [application endBackgroundTask:backgroundTaskId];



    backgroundTaskId = UIBackgroundTaskInvalid;
        }

    }