At work we were recently asked to redact the content of Lock Screen widgets when the device is locked, similar to the Activity Rings widget, which does not show your daily progress when you lock your device. It turned its not that obvious how to achieve that. With this small post I want to highlight whats necessary to achieve the same behavior as the Activity Rings widget.
Before directly dive in, its worth to mention that users, independently from what your widget offers in terms of protecting the privacy, can decide to redact all widgets when the device is locked. In order to achieve that, navigate to:
Face ID & Passcode and then toggle
Lock Screen Widgets under
Allow access when locked. This will redact every Lock Screen widget, regardless of its implementation when the
.privacySenstive(_:)(doc) is used.
As we have seen, users can choose their own settings when it comes to redaction. But sometimes its necessary that we do not want to rely on the user activating some hidden toggle deep in the iOS settings. In order to always redact your widgets view when the device is locked, you need to setup two things:
- Opt in to
NSFileProtectionComplete(doc) and add this to an
Entitlementsfile of your widget code.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
NSFileProtectionComplete will only properly work on a real device. In the simulator it does not have any effect.
- Add the
privacySensitive(_)modifier to your widgets view code.
Text("My Widget View")
Now try to run your widget on a real device. You should now experience a similar behavior like shown in the following video.
With these two additions it is possible to redact a Lock Screen widgets view as soon as the device is locked. This works completely independent from any setting the user has done and ensures that your logic is applied correctly without any side effects.
I hope this small post can help to implement the Lock Screen Redaction you need for your next widget.
See you next time ! 👋