applicationDidBecomeActive firing on deactivation on iPhone X

北城以北 提交于 2019-12-04 04:37:22

问题


Is anyone else having trouble with applicationDidBecomeActive incorrectly firing on deactivation of the app on the new iPhone X?

Here's my test app:

class ViewController: UIViewController {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(fired),
            name: .UIApplicationDidBecomeActive,
            object: nil
        )
    }

    @objc func fired(_:Any) {
        print("fired")
    }

}

Run the app on the iPhone X simulator. Naturally, I see "fired" in the console. So far, so good. Now swipe the home indicator sideways or up, to switch to a different app or to bring up the app switcher. I see "fired" appear again in the console!

This seems just wrong, and is throwing all of my apps into a kerfuffle. How can I cope with getting an activation notification on deactivation?


回答1:


In fact there are three notifications fired in rapid succession when the application is deactivated in the iPhone X Simulator:

  1. UIApplicationWillResignActive
  2. UIApplicationDidBecomeActive
  3. UIApplicationWillResignActive

This is wrong (and you might want to file a bug report), but it is something that could happen if a user deactivates - activates – deactivates the app quickly, so the app should cope with that situation anyway.

Both notifications must be handled in a symmetric fashion (and they come properly balanced, even with that bug): Actions done on "activate" must be reversed on "deactivate".



来源:https://stackoverflow.com/questions/46350524/applicationdidbecomeactive-firing-on-deactivation-on-iphone-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!