Can we detect whether a user left through the home button or lock button without listening to darwin notifications?

前端 未结 2 1699
不知归路
不知归路 2020-12-01 12:08

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 n

2条回答
  •  有刺的猬
    2020-12-01 12:56

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

    Inside of this delegate (And it will only work when called in this delegate)

    func applicationDidEnterBackground(_ application: UIApplication) {
    
    }
    

    You can call this little snippet here:

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

    Usage:

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

    EXPLANATION:

    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.

提交回复
热议问题