iosswiftiphonexcodeauto-lock

How detect the phone has been locked because of autolock time exceeded in an app?


I need to know if there is an event in the AppDelegate.swift that is triggered when the phone has been locked because of the autolock time exceeded

I have tried to use the applicationDidEnterBackground method but It is not triggered because in that case the app is still in foreground


Solution

  • You can implement applicationWillResignActive method in AppDelegate and check if brightness is changed to 0.0 as below,

    func applicationWillResignActive(_ application: UIApplication) {
        if UIScreen.main.brightness == 0.0 {
            print("App is locked!")
        }
    }