检测iPhone中断类型

发布于 2024-08-14 17:56:14 字数 228 浏览 7 评论 0原文

我可以使用 applicationWillResignActive 和 applicationDidBecomeActive 检测 iPhone 进入睡眠状态并从睡眠状态恢复。但我如何找出它是什么类型的中断。我正在制作一个音频播放器应用程序,需要在 iPhone 进入睡眠状态时保持音频播放(我知道该怎么做)。但当出现消息、警报或低电量中断时,我需要中断音频。另外,我需要在活动结束时恢复音频。

那么我如何区分这些不同的中断。

I can detect that the iPhone went to sleep and came back from sleep, by using the applicationWillResignActive and applicationDidBecomeActive. But how do I find out what kind of interrupt it was. I am making an audio player application, and need to keep the audio playing when the iPhone goes to sleep (which I know how to do). But I need to interrupt the audio when a message, alarm or low battery interrupt occurs. Also I need to resume the audio when the event is over.

So how do I differentiate between these different interrupts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

您的应用程序可能无法获取该信息,但可以尝试以下一些操作。

  1. applicationWillResignActive:中,检查NSNotification的对象和userInfo属性,看看是否有任何提示。

  2. 注册以接收发布到默认通知中心的所有通知:

    [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(didReceiveNotification:) name:nil object:nil];
    

发布任何内容时都会调用您的方法。记录通知对象和 userInfo 字典,也许您会看到发布的有用通知。如果您找到了,您可以为此进行注册。

  1. 这是最棘手的,但如果它是消息或电池警告,您可能能够访问显示的警报。警报显示在应用程序主 UIWindow 上方的 UIWindow 中。您可以注册 UIWindowDidBecomeVisibleNotification,然后查看窗口的子视图以查看是否可以找到警报或其他有用的线索。

上述所有方法都将依赖于未记录的行为来工作,并且可能会导致您的提交被 App Store 拒绝。它们都不涉及私有方法调用,尽管您可能会认为观察未记录的通知名称也算作私有 API。最终,苹果的意见是唯一重要的。

就我个人而言,我会尝试一下,确保在系统发生变化时代码能够正常失败。

That information is probably not available to your app, but here's some things to try.

  1. In applicationWillResignActive:, check the NSNotification's object and userInfo properties to see if there are any hints there.

  2. Register to receive all notifications posted to the default notification center:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotification:) name:nil object:nil];
    

Your method will be called when anything is posted. Log the notification object and userInfo dictionary and maybe you will see a useful notification being posted. If you find one, you can register just for that.

  1. This is the most hacky, but you might be able to get access to the alert that is displayed if it is a message or battery warning. Alerts are displayed in a UIWindow over your app's main UIWindow. You could register for UIWindowDidBecomeVisibleNotification, then look at the window's subviews to see if you can find an alert or some other useful clue.

All of the above methods would be relying on undocumented behavior to work, and could possibly get your submission rejected from the App Store. None of them involve private method calls, though you could argue that observing an undocumented notification name counts as private API. In the end, Apple's opinion is the only one that will matter.

Personally, I'd try it, making sure the code fails gracefully if and when the system changes.

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