简体   繁体   English

自动锁定ios设备,NSTimer和scheduledTimerWithTimeInterval

[英]autolocking ios device, NSTimer and scheduledTimerWithTimeInterval

I would like to trigger action after some ammount of time (in production that will be 30 minutes) and right now I'm using NSTimer s scheduledTimerWithTimeInterval . 我想在一段时间后(生产中将是30分钟)触发操作,现在我正在使用NSTimerscheduledTimerWithTimeInterval During tests (with timeout being 20 seconds, not 1800 seconds) everything seems fine. 在测试期间(超时为20秒,而不是1800秒)一切似乎都很好。 In debbuging mode (running from XCode) everything seems fine as well, because the device does not autolock. 在debbuging模式下(从XCode运行)一切似乎都很好,因为设备没有自动锁定。 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. 当然我可以在UIApplication sharedApplication禁用idleTimer但是当应用程序进入后台模式时,ipad仍然可以自动锁定。

Actually you can, 其实你可以,

First of all, create a default value for "startTimeOfMyExoticTimer" to NSUserDefaults: 首先,为NSUserDefaults创建“startTimeOfMyExoticTimer”的默认值:

[[[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 这种方法对我有用,希望它对你也有用

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM