【问题标题】:Is it possible to distinguish between locking the device and sending an app to background for iOS 10 and swift?是否可以区分锁定设备和将应用程序发送到 iOS 10 和 swift 的后台?
【发布时间】:2017-05-02 07:39:48
【问题描述】:

我的问题是针对 iOS 5 found answer,但是 iOS10 呢?可以在 Swift3 上实现吗? 对于 iOS 5:

// Pressing the home button
Will resign active.
Did enter background.
// Tapping app icon on Springboard
Will enter foreground.
Did become active.

// Pressing the lock button
Will resign active.
Did enter background.
// Unlocking the device
Will enter foreground.
Did become active.

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    是的,您可以,当屏幕锁定时,会调用名为 com.apple.springboard.lockcomplete 的通知。

        CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                        nil, { (_, observer, name, _, _) in
                                                print("Locked") 
                                             },
                                        "com.apple.springboard.lockcomplete" as CFString!,
                                        nil, deliverImmediately)
    

    为了检测后台/前台模式,您需要监听通知。

     NotificationCenter.default.addObserver(self, selector:#selector(handleForegroundMode), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
     NotificationCenter.default.addObserver(self, selector:#selector(handleBackgroundMode), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
    

    【讨论】:

    • 你不能再使用观察这个通知了,你的应用会因为苹果改进了他们的应用扫描工具而被拒绝。
    • 哇,这对我来说很新鲜,感谢@evanchin 提供的信息
    【解决方案2】:

    请不要观察“com.apple.springboard.lockcomplete”通知。 苹果最近改进了他们的应用扫描工具,通知是私有API的一部分,如果你仍然观察到这个通知,你的应用会在提交过程中被拒绝。

    【讨论】:

      猜你喜欢
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      相关资源
      最近更新 更多