How to Hide iOS 14 Widget Content when iPhone is locked?

Viewed 704

I have a widget that displays some sensitive data. We know that the iOS widgets can be placed both on the home screen and the Today widget page.

Today widget page page can be visible even from the locked state! Here is the problem that arises, I want to hide the content if the user locked their mobile.

I can see this behavior has done in the iOS Screen Time widget. If we locked the mobile no data is exposed. Once if I unlock (AKA activate the Face ID), All the data is visible.

Locked State

Locked State

Unlocked State

Unlocked State

1 Answers

You do it by enabling Data Protection Capability

https://developer.apple.com/documentation/widgetkit/creating-a-widget-extension

You need to set the Data Protection Entitlement

NSFileProtectionComplete or NSFileProtectionCompleteUnlessOpen, and the device is locked.

https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_default-data-protection

In Swift 5.5 iOS 15+ there is a new ViewModifier

.privacySensitive(true)

Just add it to the View that has the sensitive content

https://developer.apple.com/videos/play/wwdc2021/10018/

Related