我有一个倒计时器,它通过 AVAudioPlayer 每秒都会发出“蜂鸣声”。
我想设置一个用户选项以通过开/关开关保持应用程序唤醒。
我知道为了使应用程序保持唤醒状态,您需要设置
[UIApplication共享应用程序].idleTimerDisabled = YES;
但是我如何通过开关控制"is"或“否”选项?另外,我注意到,在使应用程序保持唤醒状态超过 5 分钟后,音频(在我的情况下为嘟嘟声)被静音?这是为什么?我怎样才能避免它?
最佳答案
你需要一个 UISwitch。只需将以下方法连接到您的 .m:
- (void) switchChanged:(id)sender {
UISwitch* switch = sender;
[UIApplication sharedApplication].idleTimerDisabled = switch.on;
}
当然,您需要首先设置正确的值:
mySwitch.on = [UIApplication sharedApplication].idleTimerDisabled;
为了完成,您必须将以下行添加到 .h 中并将它们连接到 .nib 文件中:
@interface YourClass{
...
IBOutlet UISwitch* mySwitch;
}
- (void) switchChanged:(id)sender;
关于iPhone : User option to keep the app awake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7743589/