You are currently viewing How to Hide Sensitive Widget Data When Device Is Locked?

How to Hide Sensitive Widget Data When Device Is Locked?

With the introduction of widgets in iOS, users can now easily access information from their favorite apps right at the lock screen and Today View. As good as it might seem, this does introduce privacy issues whereby sensitive data can become accessible even though the device is locked.

To address this issue, we can take the following approaches:

  1. Partial privacy redactions
  2. Full privacy redactions

In this article, we will go through each of these approaches. So let’s get right into it!


Partial Privacy Redactions

As the name implies, Partial Privacy Redactions will only redact some of the UI elements in the widgets. Let’s say we have a Bitcoin wallet widget that shows the user’s current Bitcoin balance.

var body: some View {
    
    VStack(alignment: .leading) {

        Text("Bitcoin Balance")
            .font(.title2)
            .fontWeight(.bold)
            .foregroundColor(.orange)
        
        // Show Bitcoin balance
        Text("0.25₿")
            .font(.headline)
            .fontWeight(.semibold)
            .foregroundColor(.gray)
    }
    .containerBackground(for: .widget) { }
}

As a developer, it is our responsibility to protect the privacy of our users, thus it makes sense for us to redact the balance value when the device is locked. To achieve that, we can use the .privacySensitive() modifier introduced in iOS 15.

Hiding sensitive data on widgets using the privacySensitive() modifier
Using the .privacySensitive() modifier

Note:

Partial privacy redactions only work on real devices, so make sure not to test them using the simulator.

If you are using a device running on iOS 16, be sure to go to “Settings” → “Face ID & Passcode” and turn off the “Lock Screen Widgets” toggle in the “Allow Access When Locked” section. Or else, the .privacySensitive() modifier will not have any effect.

Turn off "allow access when locked" for lock screen widgets in iOS
Turn off “allow access when locked” for lock screen widgets

That’s it for partial privacy redactions. Pretty simple isn’t it? 🤩


Full Privacy Redactions

Another way to hide sensitive data on widgets is by leveraging complete data protection. When complete data protection is enabled, WidgetKit will automatically replace your widget’s timeline content with the placeholder content as long as the device is locked.

To enable complete data protection for a widget, we need to first activate the data protection capability in the Apple Developer Portal.

  1. Go to the identifiers page
  2. Add or select the identifier that represents your widget
  3. Enable the “Data Protection” capability and save
  4. Navigate to the profiles page
  5. Create or select the corresponding provisioning profile
  6. Download the profile & double-click to add it to Xcode
Adding "Data Protection" capability in Apple Developer Portal
Adding “Data Protection” capability in Apple Developer Portal

Once done, head over to Xcode and assign the provisioning profile we just downloaded to your widget target.

Assigning provisioning profile in Xcode
Assign provisioning profile with “Data Protection” enabled

After that, add the “Data Protection” capability to your widget target.

Adding "Data Protection" capability in Xcode
Adding “Data Protection” capability in Xcode

An entitlement file will be created after adding the capability, open it and make sure the data protection level is set to NSFileProtectionComplete.

With that, all data in your widgets will be redacted when the device is locked. Here’s a screenshot of our Bitcoin wallet widget in full privacy redactions mode.

iOS widgets in full privacy redactions mode
Bitcoin wallet widget in full privacy redactions mode

Notice that for full privacy redactions, the .privacySensitive() modifier is not required.

Note:

Full privacy redactions only work on real devices, so make sure not to test them using the simulator.


Now that you have a clear understanding of how to hide sensitive data on widgets, be sure to take your user’s privacy into consideration when creating widgets.

I hope you enjoy reading this article, if you do, feel free to follow me on Twitter and LinkedIn. You can also subscribe to my newsletter so that you won’t miss out on any of my upcoming iOS development-related articles.

Thanks for reading. 👨🏻‍💻


👋🏻 Hey!

While you’re still here, why not check out some of my favorite Mac tools on Setapp? They will definitely help improve your day-to-day productivity. Additionally, doing so will also help support my work.

  • Bartender: Superpower your menu bar and take full control over your menu bar items.
  • CleanShot X: The best screen capture app I’ve ever used.
  • PixelSnap: Measure on-screen elements with ease and precision.
  • iStat Menus: Track CPU, GPU, sensors, and more, all in one convenient tool.