Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS10 Today Widget - how to determine if iPhone is locked or not?

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:

enter image description here

And here's what the widget looks like when the iPhone is unlocked:

enter image description here

like image 806
beeef Avatar asked Nov 09 '22 08:11

beeef


1 Answers

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.

like image 124
João Eduardo Avatar answered Nov 14 '22 22:11

João Eduardo