Dom's Bytes

On Apple Screen Time API

Recently I’ve been working on integrating Apple Screen Time into Brave iOS in order to:

  • track web pages usage to get a detailed breakdown in the Screen Time app Screen Time app
  • enforce app limits on web pages Screen Time limit reached view

The implementation is fairly simple but I’ve learned a lot about Screen Time API and Safari’s behaviour along the way.

Screen Time API

Screen Time API actually consists of three frameworks:

It also includes STWebpageController which allows you to implement the infamous You’ve reached your limit view you see in Safari when web content limits are set. Unlike the frameworks above, it does not require any additional permission to work.

import ScreenTime

let screenTimeController = STWebpageController()

let url: URL? {
    willSet {
        screenTimeController.url = newValue
    }
}

You add the controller as a subview, set STWebpageController#url whenever a user switches tabs, and you are ready to go!

To emulate Safari’s behaviour, STWebpageController#suppressUsageRecording should be set to true when the user is in incognito mode (see Web content limits in Safari).

Testing STWebpageController via the Simulator

The XCode Simulator does not fully implement the Screen Time API. You can set app limits in the device settings app but the controller won’t work correctly.

Content & Privacy restrictions

iOS allows you to restrict access to websites. It will display a You cannot browse this page view whenever you visit a restricted website. You might think you have to implement this feature yourself but you don’t: this functionality is built into WebKit, therefore it’s already implemented on all iOS web browsers.

Web content limits in Safari

Although Safari displays the You’ve reached your limit view even for private tabs, it does it only if you ran out of time while browsing the page in a public tab. It does not track web page usage in incognito mode.

Each Safari tab has its own STWebpageController hence the “limit reached” view does not prevent you from switching between tabs.