我在问题上为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.
最佳答案
是的,您可以,当屏幕锁定时,将调用名为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)