Helle there, I'm using the code below to check whether the device's screen is on or off. I got this code from this SO post.
Code:
@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 {
}
But I got this error:
Cannot convert value of type '(CFNotificationCenter, UnsafeMutableRawPointer?, CFString, UnsafeRawPointer?, CFDictionary) -> Void' to expected argument type 'CFNotificationCallback!' (aka 'ImplicitlyUnwrappedOptional<@convention(c) (Optional, Optional, Optional, Optional, Optional) -> ()>')
Does anyone know what I'm doing wrong with the displayStatusChanged
function? Any help, suggestions or link will be appreciated.
Thanks
According to the documentation:
center
parameter should be optional (i.e. missing ?
),name
you have CFString
but the documentation says CFNotificationName?
CFDictionary
should be optional (you're missing a ?
), Should fix your error if you match your parameters to the expected types.
func displayStatusChanged(center: CFNotificationCenter?, observer: UnsafeMutableRawPointer?, name: CFNotificationName?, object: UnsafeRawPointer?, userInfo: CFDictionary?) -> Void {
}