在iOS中检查“设备”屏幕的打开或关闭

时间:2018-06-21 13:34:35

标签: ios swift3 cfnotification

在这里,我正在使用下面的代码检查设备的屏幕是打开还是关闭。我从this SO post获得了这段代码。

代码:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), nil, displayStatusChanged, "com.apple.springboard.lockcomplete", nil, CFNotificationSuspensionBehavior.deliverImmediately)
        //CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), nil, displayStatusChanged, "com.apple.springboard.lockstate", nil, CFNotificationSuspensionBehavior.deliverImmediately)

        return true
    }
}//AppDelegate class end here

func displayStatusChanged(center:CFNotificationCenter,observer: UnsafeMutableRawPointer?,name:CFString,object: UnsafeRawPointer?,userInfo:CFDictionary) -> Void {

}

但是我得到了这个错误:

  

无法转换类型为'(CFNotificationCenter,   UnsafeMutableRawPointer?,CFString,UnsafeRawPointer?,CFDictionary)   ->无效”到预期的参数类型“ CFNotificationCallback!” (又名'ImplicitlyUnwrappedOptional <@convention(c)   (可选,可选,   可选,可选,   可选)->()>')

有人知道displayStatusChanged函数在做什么吗?任何帮助,建议或链接将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:1)

根据the documentation

  • center参数应该是可选的(即缺少?),
  • 对于name,您有CFString,但文档中显示CFNotificationName?
  • ,并且CFDictionary应该是可选的(您缺少?),

如果您将参数与期望的类型匹配,则应该修复错误。

func displayStatusChanged(center: CFNotificationCenter?, observer: UnsafeMutableRawPointer?, name: CFNotificationName?, object: UnsafeRawPointer?, userInfo: CFDictionary?) -> Void {

}