我在问题上找到了iOS 5的答案,但是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.
是的,您可以,当屏幕锁定通知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)
请不要观察" com.apple.springboard.lockcomplete"通知。Apple最近完善了他们的应用程序扫描仪工具,该通知是私人API的一部分,如果您仍然观察到此通知,您的应用程序将在提交期间被拒绝。