2012-08-27 68 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' - 鎖定設備

相關問題