当 iPhone 处于睡眠模式时,iPhone 应用程序延迟 10 -15 分钟
我创建了一个使用 NSTimer 的应用程序,它每秒都会触发一次。
我的问题是,如果 Iphone 处于睡眠模式,我会延迟 10 到 15 事件触发前的分钟数。我已经 stackoverflowed 并用谷歌搜索了这个 其原因似乎是手机在某些情况下停止监听 处于睡眠模式时的事件。
有些人通过播放静音来解决这个问题,不允许 打电话睡觉。
- 延迟的原因可能是什么?
- 静音解决方案似乎是一种非常“肮脏”的解决方案。还有其他方法可以解决这个问题吗?
- 如果我使用静音解决方案会通过苹果审核吗?
代码:
timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(goAction)userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
-(void)goAction {
// Here i check for some dates and then call the activateBeepAlarmView
}
I have created an app that uses NSTimer, which gets triggered each second.
My problem is that if the Iphone is in sleep mode i get a delay for 10 to 15
minutes before the event is triggered. I have stackoverflowed and googled this
and the reason for this seems to be that the phone stops listening for certain
events when in sleep mode.
Some people have solved this issue by playing a mute sound, not allowing the
phone to sleep.
- What could be the reason for the delay?
- The mute sound solution seems to be a very "dirty" one. Is there some other way to solve this?
- If I use the mute sound solution will it the pass the apple review?
Code:
timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(goAction)userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
-(void)goAction {
// Here i check for some dates and then call the activateBeepAlarmView
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,既然没有人回答我的三个问题,我就必须回答它们:
1。延迟的原因可能是什么?
我必须引用 Ben S:
2.静音解决方案似乎是一种非常“肮脏”的解决方案。还有其他方法可以解决这个问题吗?
不,目前我还没有找到其他解决方案,如果我错了,请随时纠正我。
看看这个博客文章如何做到这一点。
3.如果我使用静音方案能通过苹果审核吗?
是的
Well since no one has answered my three questions I will have to answer them:
1. What could be the reason for the delay?
I will have to quote Ben S:
2. The mute sound solution seems to be a very "dirty" one. Is there some other way to solve this?
No, currently I haven't found another solution, please feel free to correct me if I'm wrong.
Check out this blog post how to do it.
3. If I use the mute sound solution will it the pass the apple review?
Yes
当 iPhone 进入睡眠状态时,您的应用程序和运行 NSTimer 的 runloop 也会进入睡眠状态。
您似乎认为 NSTimer 是某种基于硬件的计时器。它不是。它完全在启动它的应用程序的软件内运行。我不知道是什么唤醒了你的应用程序,但它绝对不是 NSTimer。
简而言之,你想做的事情是不可能的。您无法让手机进入睡眠状态,然后应用程序仍处于活动状态并运行。静音技术只是保持手机唤醒和应用程序运行的一个杂凑。
如果您需要手机保持唤醒状态,则需要将应用程序的
idleTimerDisabled
设置为YES。这将防止手机进入睡眠状态,并且应用程序可以保持活动状态。但是一旦你让手机进入睡眠状态,它就无法从应用程序代码中唤醒。只有硬件可以响应警报或传入消息来执行此操作。When the iPhone goes to sleep, so does your app and the runloop that runs the NSTimer.
You seem to think that an NSTimer is some sort of hardware based timer. It is not. It operates completely within the software of the app that launches it. I don't know what is waking your app up but it is definitely not the NSTimer.
In short, what you want to do is impossible. You can't sleep the phone and then have an app still active and running. The mute sound technique is just a kludge to keep the phone awake and the app running.
If you need the phone to stay awake, you need to set the application's
idleTimerDisabled
to YES. This will prevent the phone from sleeping and the app can remain active. But once you let the phone sleep, it cannot be awaken from app code. Only the hardware can do that in response to an alarm or an incoming message.当 NSTimer 每秒触发一次时,您的应用程序会发生什么?请提供显示计时器创建的代码以及计时器完成时调用的选择器的代码。
另外,“延迟 10 到 15 分钟”是什么意思?延迟是否总是那么长,或者是您等待唤醒 iPhone 然后触发事件的时间?
根据您每秒钟需要做什么,您可以用不同的方式处理这种情况。请回复我们,我们将尽力解决这个问题。
巴特
What happens in your app when the NSTimer is triggered each second? Please provide code showing the creation of the timer as well as the code for the selector that is called when the timer completes.
Also what do you mean by a "delay for 10 to 15 minutes"? Is the delay always that long or is that how long you wait to awaken the iPhone and then the event is triggered?
Depending on what you need to do every second you can handle this situation in different ways. Please respond and we'll try to work our way through this.
Bart
@Jakob,
这对于“官方 SDK”是不可能的。如果您正在为越狱手机开发应用程序,那么您可以使用 IOKit 框架。有关更多信息,请参阅此。
@Jakob,
This is impossible with the "Official SDK". If you're developing apps for jail broken phone, then you can use IOKit framework for this. For more info please refer this.