我们可以检测用户是否通过主页按钮或锁定按钮而不听darwin通知吗? [英] Can we detect whether a user left through the home button or lock button without listening to darwin notifications?

查看:126
本文介绍了我们可以检测用户是否通过主页按钮或锁定按钮而不听darwin通知吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近向应用商店提交了一个新的二进制文件并将其发送给审核,并立即被拒绝,并显示以下消息。 不支持的操作 - 不允许应用程序监听设备锁定通知。经过一番挖掘,我发现我们不能使用com.apple.springboard.lockstate找出锁定状态。

I recently submitted a new binary to the app store and sent it in for review and it was immediately rejected with the following message. "Unsupported operation - Apps are not allowed to listen to device lock notifications.". After some digging around I found out that we can't use "com.apple.springboard.lockstate" to figure out the lock state.

基本上,我的应用程序需要知道用户如何离开我的应用程序。是否按下主页按钮,锁定按钮,通过点击另一个应用程序的通知等离开应用程序,有没有办法实现这一目标?我开始调查,看看我的应用程序是否在后台运行,也许我们可以检查应用程序状态来弄清楚。那就是我得到的,我想知道是否有人对此有更深入的了解

Essentially, my app needs to know how the user left my app. Whether it was pressing the home button, lock button, leaving the app through hitting another app's notification, etc. Is there any way of achieving this? I started investigating to see if my app were to run in the background, maybe we could check the application state to figure it out. That's as far as I got, I was wondering if anyone had anymore insight on this

推荐答案

在搜索了来自apple和通过大量的线程挖掘,我想我可能偶然发现了解决方案。

After searching through the documentation from apple and digging through a ton of threads I think I might have stumbled upon the solution.

据我所知,目前这是检测用户是否通过主页按钮或锁定按钮(我不相信这在模拟器上有效,你必须在实际的手机上试试)。

As far as I know this is currently the only way of detecting whether a user left through the home button or lock button (I don't believe this works on the simulator you have to try it on an actual phone).

在这个代表内部(并且只有在此委托中调用时才会起作用)

func applicationDidEnterBackground(_ application: UIApplication) {

}

您可以在此处调用此小片段:

func DidUserPressLockButton() -> Bool {
    let oldBrightness = UIScreen.main.brightness
    UIScreen.main.brightness = oldBrightness + (oldBrightness <= 0.01 ? (0.01) : (-0.01))
    return oldBrightness != UIScreen.main.brightness
}

用法:

func applicationDidEnterBackground(_ application: UIApplication) {
    if (DidUserPressLockButton()) {
        //User pressed lock button
    } else {
        //user pressed home button
    }
}

说明:

苹果似乎只允许你从更改屏幕亮度applicationDidEnterBackground 当用户离开锁定按钮而不是主页按钮时。因此,我们的想法是以微不足道的数量更改屏幕亮度,并检查它是否能够更改。这看起来有点像hacky,但我听到这实际上是按预期工作的。至于测试它似乎100%的时间都在工作。除了真正想要改变屏幕亮度的用户之外,我找不到任何问题。我希望其他人可以找到一些不那么hacky和更具体的东西。

It seems that apple only lets you change the screen brightness from applicationDidEnterBackground when the user left through the lock button and not the home button. So the idea is to change the screen brightness with a miniscule amount and check to see if it was able to be changed. This seems kinda hacky but I've heard that this is actually working as intended. As far as testing it goes it seems to be working 100% of the time. I could not find any issues with this except for users that really do want to change the brightness of the screen. I hope someone else can find something less hacky and more concrete.

这篇关于我们可以检测用户是否通过主页按钮或锁定按钮而不听darwin通知吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆