当 iPhone 处于睡眠模式时,iPhone 应用程序延迟 10 -15 分钟

发布于 2024-08-23 17:37:33 字数 624 浏览 4 评论 0原文

我创建了一个使用 NSTimer 的应用程序,它每秒都会触发一次。

我的问题是,如果 Iphone 处于睡眠模式,我会延迟 10 到 15 事件触发前的分钟数。我已经 stackoverflowed 并用谷歌搜索了这个 其原因似乎是手机在某些情况下停止监听 处于睡眠模式时的事件。

有些人通过播放静音来解决这个问题,不允许 打电话睡觉。

  1. 延迟的原因可能是什么?
  2. 静音解决方案似乎是一种非常“肮脏”的解决方案。还有其他方法可以解决这个问题吗?
  3. 如果我使用静音解决方案会通过苹果审核吗?

代码:

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.

  1. What could be the reason for the delay?
  2. The mute sound solution seems to be a very "dirty" one. Is there some other way to solve this?
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

把时间冻结 2024-08-30 17:37:33

好吧,既然没有人回答我的三个问题,我就必须回答它们:

1。延迟的原因可能是什么?
我必须引用 Ben S

一旦 applicationWillResignActive 在您的应用程序上被调用,您只需停止接收事件:
当应用程序从活动状态转换到非活动状态时,委托可以实现此方法来进行调整。当应用程序处于非活动状态时,它正在执行但不调度传入事件。当弹出覆盖窗口或设备锁定时会发生这种情况。

睡眠模式的目的是节省能源。为此,设备将停止侦听您所请求的事件。 NSTimer 事件仍然会触发,因为它们不需要昂贵的(电池方面的)硬件监控。此外,闹钟是使用 NSTimer 实现的,因此即使在睡眠时它们也需要能够运行。否则,人们可能不会醒来并责怪他们的 iPhone。

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:

Once applicationWillResignActive gets called on your application you simply stop receiving events:
The delegate can implement this method to make adjustments when the application transitions from an active state to an inactive state. When an application is inactive, it is executing but is not dispatching incoming events. This occurs when an overlay window pops up or when the device is locked.

The point of sleep mode is to save energy. To do so, the device stops listening for events like the ones you're asking for. NSTimer events will still fire since they don't require expensive (battery-wise) hardware monitoring. Also, alarms are implemented using NSTimer, so they need to be able to function even when in sleep. Otherwise, people might not wake up and blame their iPhone.

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

以酷 2024-08-30 17:37:33

当 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.

念﹏祤嫣 2024-08-30 17:37:33

当 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

诠释孤独 2024-08-30 17:37:33

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文