屏幕locking时我们如何让我们的应用程序运行

设备locking有问题。 如果我的应用程序正在运行,设备被locking,那么我的应用程序也无法正常工作 即使设备被locking,我也希望我的应用能够正常工作。 我的代码如下:

- (void)applicationDidEnterBackground:(UIApplication *)application { [[UIApplication sharedApplication] setIdleTimerDisabled:NO]; background = YES; UIApplication *app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ if (background) { StressFreeAlarmViewController *alarmController=[[StressFreeAlarmViewController alloc] initWithNibName:@"StressFreeAlarmViewController" bundle:nil]; [alarmController setTimer:[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updatingApp) userInfo:nil repeats:YES]]; background=NO; } }); } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. background = NO; } 

注释这一行

  [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; here UIApplication *app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ //[app endBackgroundTask:bgTask]; //bgTask = UIBackgroundTaskInvalid; }]; 

设备被locking时,无法安装应用程序。 但是,当应用程序已经在设备中,你可以像上面的post一样通过preventSleepTimer框架来防止locking

当设备被locking时,错误信息将是:错误:无法启动'/Users/venkateswarlun/Library/Developer/Xcode/DerivedData/XXXXXX-celefkdlufzfpexcvbngfwhpwosr/Build/Products/Debug-iphoneos/XXXXXX.app/XXXXXX' – devicelocking

为此,您需要防止屏幕locking以下代码被使用

 - (void)startPreventSleep { // We need to play a sound at least every 10 seconds to keep the iPhone awake. // We create a new repeating timer, that begins firing now and then every ten seconds. // Every time it fires, it calls -playPreventSleepSound self.preventSleepTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:0] interval:10.0 target:self selector:@selector(playPreventSleepSound) userInfo:nil repeats:YES]; // We add this timer to the current run loop NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; [runLoop addTimer:self.preventSleepTimer forMode:NSDefaultRunLoopMode]; } 

stopPreventSleep停止睡眠预防。

 - (void)stopPreventSleep { [self.preventSleepTimer invalidate]; self.preventSleepTimer = nil; } 

有关更多详细信息,请参阅此处的链接。