簡體   English   中英

在iOS中檢查設備屏幕的打開或關閉

[英]Check Device screen On or Off in iOS

嗨,在這里,我正在使用下面的代碼檢查設備的屏幕是打開還是關閉。 我從這篇SO帖子中獲得了這段代碼。

碼:

@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函數在做什么嗎? 任何幫助,建議或鏈接將不勝感激。

謝謝

根據文檔

  • center參數應該是可選的(即缺少? ),
  • 您有CFString作為name ,但文檔中顯示CFNotificationName?
  • 並且CFDictionary應該是可選的(您缺少? ),

如果您將參數與期望的類型匹配,應該可以解決您的錯誤。

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM