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

3 回答 3

1

当设备被锁定时,您无法安装应用程序。但是当应用程序已经在设备中时,您可以通过 preventSleepTimer 框架防止锁定,如上面的帖子

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

于 2012-09-04T08:25:46.750 回答
1

评论这一行

 [app endBackgroundTask:bgTask];
 bgTask = UIBackgroundTaskInvalid;


here


 UIApplication  *app = [UIApplication sharedApplication];

 bgTask  = [app beginBackgroundTaskWithExpirationHandler:^{
  //[app endBackgroundTask:bgTask];
  //bgTask = UIBackgroundTaskInvalid;
 }];
于 2012-08-27T12:09:00.633 回答
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;
}

有关更多详细信息,您可以参考此处的链接。

于 2012-08-27T12:07:59.467 回答