Ive been using stack overflow for awhile and its now a moment where I will need help.
So I’m working on a project in Swift and one of the main things that I need to know at times is if the device is locked or unlocked. This is a music player app and when the app goes into lock mode it will follow a certain flow. Currently I have some functions that will check these things and it flows as such:
- There are two listeners using
UIApplication.protectedDataDidBecomeAvailableNotification
andUIApplication.protectedDataWillBecomeUnavailableNotification
to check to see if the user presses the sleep/wake button on their device - There is a function which will check to get the current lock status of the phone and return it by checking
UIApplication.shared.isProtectedDataAvailable
and returning the bool – according to apples documentation The phone is unlocked if this is true, if false the phone is locked
Now my issue – clearly when a user is using the app it won’t always be in the foreground and it can be in the background 99% of the time for most users, however with this API isProtectedDataAvailable
when I swipe my app into the background it won’t return the correct value regardless of the listeners I have setup. When the app is in the foreground it is 100% accurate.
As I am new to iOS development I am not too sure what to try, Ive tried using a DispatchQueue expecting the idea that there may be a delay however it doesn’t work. I don’t know what else to try so I was hoping others could help and give me some ideas
1