Teabyte

Mobile app development for iOS and Swift

Redact Lock Screen Widgets when the device is locked

2022-11-19

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.

User Defined Redaction

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:

Settings -> 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.

Custom Redaction when device is locked

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:

  1. Opt in to NSFileProtectionComplete(doc) and add this to an Entitlements file of your widget code.
App.entitlements
<?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">
<plist version="1.0">
<dict>
	<key>com.apple.developer.default-data-protection</key>
	<string>NSFileProtectionComplete</string>
</dict>
</plist>

⚠️ NSFileProtectionComplete will only properly work on a real device. In the simulator it does not have any effect.

  1. Add the privacySensitive(_) modifier to your widgets view code.
MyView.swift
Text("My Widget View")
	.privacySensitive()

Now try to run your widget on a real device. You should now experience a similar behavior like shown in the following video.

Conclusion

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 ! 👋