在 Swift 中的 iOS9 和 iOS10 上检测屏幕锁定或主页按钮按下 3

Detect screen lock or home button press on iOS9 and iOS10 in Swift 3

Rean Wen 在这个 post 中提供的答案在 Swift 2.2 中工作得很好,但现在我升级到 XCode 8.0 和 Swift 3 但它没有工作了。

我收到以下错误消息:

.../project/AppDelegate.swift:41:108: Cannot convert value of type '(@convention(c) (CFNotificationCenter?, UnsafeMutableRawPointer?, CFString?, UnsafeRawPointer?, CFDictionary?) -> Void)!' to expected argument type 'CFNotificationCallback!'

来自这一行:

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), nil, LockNotifierCallback.notifierProc(), "com.apple.springboard.lockcomplete", nil, CFNotificationSuspensionBehavior.DeliverImmediately)

在 AppDelegate.swift 文件中。

有谁知道如何解决这个问题?由于我刚刚开始学习 Swift,任何帮助将不胜感激。

这可能对你有用。

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                    nil,
                                    { (_, observer, name, _, _) in

                                        print("received notification: \(name)")
        },
                                    "com.apple.springboard.lockcomplete" as CFString!,
                                    nil,
                                    .deliverImmediately)

您可以查看此 answer 了解更多详情。

最近在开发任务管理APP的时候遇到了同样的问题。我尝试使用带有 CFNotificationCallback 方法的 Darwin Notification 来解决这个问题。

步骤1:

在 AppDelegate class 声明下添加以下代码

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    let displayStatusChanged: CFNotificationCallback = { center, observer, name, object, info in
        let str = name!.rawValue as CFString
        if (str == "com.apple.springboard.lockcomplete" as CFString) {
            let isDisplayStatusLocked = UserDefaults.standard
            isDisplayStatusLocked.set(true, forKey: "isDisplayStatusLocked")
            isDisplayStatusLocked.synchronize()
        }
    }

    //other functions
}
第2步:

didFinishLaunchingWithOptions内,添加以下代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let isDisplayStatusLocked = UserDefaults.standard
    isDisplayStatusLocked.set(false, forKey: "isDisplayStatusLocked")
    isDisplayStatusLocked.synchronize()

    // Darwin Notification
    let cfstr = "com.apple.springboard.lockcomplete" as CFString
    let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()
    let function = displayStatusChanged
    CFNotificationCenterAddObserver(notificationCenter,
                                    nil,
                                    function,
                                    cfstr,
                                    nil,
                                    .deliverImmediately)

    return true
}
第 3 步:

applicationDidEnterBackground内,添加以下代码:

func applicationDidEnterBackground(_ application: UIApplication) {      
    let isDisplayStatusLocked = UserDefaults.standard
    if let lock = isDisplayStatusLocked.value(forKey: "isDisplayStatusLocked"){
        // user locked screen
        if(lock as! Bool){            
            // do anything you want here
            print("Lock button pressed.")
        }
        // user pressed home button
        else{            
            // do anything you want here
            print("Home button pressed.")
        }
    }
}
第4步:

applicationWillEnterForeground内,添加以下代码:

func applicationWillEnterForeground(_ application: UIApplication) {
    print("Back to foreground.")
    //restore lock screen setting
    let isDisplayStatusLocked = UserDefaults.standard
    isDisplayStatusLocked.set(false, forKey: "isDisplayStatusLocked")
    isDisplayStatusLocked.synchronize()  
}

您可以通过演示项目查看详细方法:swift-home-vs-lock-button