I'm adding a widget to my existing iOS App. Last week iOS10 was released and added some new features to App Extensions and so to Today Widgets.
There are a few widgets from Apple, e.g. the "Activity widget" if you are using an Apple Watch to display your activity in the widget view. This widget is only working if your iPhone is unlocked. If it's locked it displays a message like "Unlock to show activity data" or something like that. How can I check if the user's phone is locked or not?
I want to display some information from HealthKit and this is not possible as long as the iPhone is locked, because Apple restricts access to health data in that case. If I unlock my iPhone everything works as expected. If I'm on the lock screen it doesn't update because access is restricted.
Apple's documentation to today widgets is pretty nice, but I couldn't find anything according to my problem.
Here's what the widget looks like when the iPhone is locked:
And here's what the widget looks like when the iPhone is unlocked:
The best way to determine whether the devices is locked is to implement the UIApplicationDelegate
, with that you can override the methods applicationProtectedDataDidBecomeAvailable
and applicationProtectedDataWillBecomeUnavailable
Then accessing the UIApplication.shared.isProtectedDataAvailable
property you can check if the device is locked or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With