9 Replies
      Latest reply on Feb 7, 2018 1:58 AM by gigaset.ios
      biondo-giuseppe Level 1 Level 1 (0 points)

        I need to detect if the screen il locked or unlocked when my app is running in background.

        I can't find documentation regarding this matter for swift programming.

        Some source reference ?

        • Re: how to detect the locked/unlocked screen status with swift
          biondo-giuseppe Level 1 Level 1 (0 points)

          I wish simply to send the notifications only when the screen is unlocked and avoid them when is locked.

            • Re: how to detect the locked/unlocked screen status with swift
              eskimo Apple Staff Apple Staff (10,235 points)

              It depends on what you mean by screen locked.  If you mean requires a passcode or Touch ID to use, you can get that state via the isProtectedDataAvailable property of UIApplication (and there are associated notifications for when it changes).  For the other common uses of the term (screen dark, screen awake but showing system UI), there are no supported APIs to detect those states.

              Share and Enjoy

              Quinn “The Eskimo!”
              Apple Developer Relations, Developer Technical Support, Core OS/Hardware
              let myEmail = "eskimo" + "1" + "@apple.com"

                • Re: how to detect the locked/unlocked screen status with swift
                  Vicente GFT Level 1 Level 1 (0 points)

                  Hi Quinn,

                   

                  Is there any way of doing the same from an extension, like a today widget? I would like to detect if the device is locked or not, and change the info displayed depending on if protected data is available or not.

                   

                  Thanks,

                   

                  Vicente

                    • Re: how to detect the locked/unlocked screen status with swift
                      eskimo Apple Staff Apple Staff (10,235 points)

                      Have you tried using the notifications provided for this (UIApplicationProtectedDataWillBecomeUnavailable and UIApplicationProtectedDataDidBecomeAvailable)?

                      Share and Enjoy

                      Quinn “The Eskimo!”
                      Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                      let myEmail = "eskimo" + "1" + "@apple.com"

                        • Re: how to detect the locked/unlocked screen status with swift
                          gdiez Level 1 Level 1 (0 points)

                          Hi,

                           

                          These notifications are available if the user use a passcode or touchID.

                           

                          Is that a way to use this without a passcode and check if the user lock and unlock his Phone ?

                          Maybe in simulating the protection or write a secure file in the app ?

                           

                          Thank you very Much.

                           

                          Best Regards Gabriel.

                            • Re: how to detect the locked/unlocked screen status with swift
                              eskimo Apple Staff Apple Staff (10,235 points)

                              Is that a way to use this without a passcode and check if the user lock and unlock his Phone ?

                              No, because these are not about screen lock they are about data protection.  My post from 28 Dec was pretty clear about the difference.

                              Share and Enjoy

                              Quinn “The Eskimo!”
                              Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                              let myEmail = "eskimo" + "1" + "@apple.com"

                            • Re: how to detect the locked/unlocked screen status with swift
                              gigaset.ios Level 1 Level 1 (0 points)

                              >> Have you tried using the notifications provided for this (UIApplicationProtectedDataWillBecomeUnavailable and UIApplicationProtectedDataDidBecomeAvailable)?

                               

                              Ok, I can get notified about the state change, but how can I find the initial state (device locked / unlocked) when my Today Widget appears? I can see that "Find Friends" widget does this for security reasons, but it's made by Apple so perhaps it uses some private APIs. Say we have a smart home solution and we want to remotely disarm the alarm or even open the door - it would be a serious problem if such functionality is available on the locked screen without passcode check, while on the other hand it's very useful if the phone is unlocked.

                                • Re: how to detect the locked/unlocked screen status with swift
                                  eskimo Apple Staff Apple Staff (10,235 points)

                                  I can see that "Find Friends" widget does this for security reasons, but it's made by Apple …

                                  Right.  Built-in system components, like Find Friends, are not subject to the iOS SDK’s constraints.

                                  how can I find the initial state (device locked / unlocked) when my Today Widget appears?

                                  That’s tricky because the protectedDataAvailable property is on UIApplication, and app extensions don’t have access to that.  The best solution I can think of is to create a dummy file (or keychain item) with the right protection class and try to access it.  That access will only work if protected data is available.

                                  Given the above it seems like an enhancement request is in order here; please post your bug number, just for the record.

                                  Share and Enjoy

                                  Quinn “The Eskimo!”
                                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                                  let myEmail = "eskimo" + "1" + "@apple.com"