嗨,在这里,我正在使用下面的代码检查设备的屏幕是打开还是关闭。我从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 (可选,可选,
可选,可选,
可选)->()>')
有人知道
displayStatusChanged
函数在做什么吗?任何帮助,建议或链接将不胜感激。谢谢
最佳答案
根据the documentation:center
参数应该是可选的(即缺少?
),
对于name
,您具有CFString
,但文档中显示CFNotificationName?
并且CFDictionary
应该是可选的(您错过了?
),
如果您将参数与期望的类型匹配,则应该修复您的错误。
func displayStatusChanged(center: CFNotificationCenter?, observer: UnsafeMutableRawPointer?, name: CFNotificationName?, object: UnsafeRawPointer?, userInfo: CFDictionary?) -> Void {
}
关于ios - 在iOS中检查设备屏幕打开或关闭,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50969923/