【问题标题】:Check Device screen On or Off in iOS在 iOS 中检查设备屏幕打开或关闭
【发布时间】:2018-06-21 21:45:09
【问题描述】:

嘿嘿,我正在使用下面的代码来检查设备的屏幕是打开还是关闭。我从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) -> Void'到预期的参数类型'CFNotificationCallback!' (又名'ImplicitlyUnwrappedOptional ()>')

有谁知道我在使用displayStatusChanged 函数时做错了什么?任何帮助、建议或链接将不胜感激。

谢谢

【问题讨论】:

  • 为什么你的displayStatusChanged 坐在你的课外?

标签: ios swift3 cfnotification


【解决方案1】:

根据the documentation:

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

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

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 2015-08-04
    • 1970-01-01
    相关资源
    最近更新 更多