是否可以区分锁定设备和将应用程序发送到iOS 10和swift的后台?

时间:2017-05-02 07:39:48

标签: ios swift

对于我的问题,对于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.

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

请不要观察" com.apple.springboard.lockcomplete"通知。 Apple最近改进了他们的应用扫描工具,通知是私有API的一部分,如果您仍然遵守此通知,您的应用将在提交期间被拒绝。