2012-08-27 56 views
0

我遇到了设备锁定问题。如果我的应用程序正在运行并且设备被锁定,那么我的应用程序也无法工作。即使设备被锁定,我也希望我的应用能够正常工作。 我的代码如下:如何在屏幕锁定时允许我们的应用程序运行

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

回答

1

评论这条线

[app endBackgroundTask:bgTask]; 
bgTask = UIBackgroundTaskInvalid; 


here 


UIApplication *app = [UIApplication sharedApplication]; 

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
    //[app endBackgroundTask:bgTask]; 
    //bgTask = UIBackgroundTaskInvalid; 
}]; 
0

对于您需要防止屏幕锁,这里下面的代码是使用

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

For More detail You can refer the Link Here.

1

设备锁定时,则无法安装应用程序。但是,当应用程序已经在设备中,你可以防止通过preventSleepTimer框架锁定,如上所述

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

相关问题