The Screen Time API and What’s new in Screen Time API

Krishna kushwaha
3 min readJul 19, 2023

--

in iOS 16, we can fetch all of the applications daily screen duration times.

in 2022, new functionality was added so the framework could also monitor the device and device activity reports. you can build apps that help people manage their relationship with their device. Screen time API use features like core restrictions, measurable control and device activity report for the device’s owner, parent and guardians…

Screen Time API introduced three new frameworks-

Family Controls-

  • Authorizes access to Screen Time API
  • Prevents removal and circumvention
  • Privacy preserving tokens for apps used by your family

Managed Settings-

  • Set persistent restrictions on device
  • Provide web content filtering
  • Shield apps and websites with custom UI

Device Activity-

  • Executes code on start and end of Device Activity Schedules
  • Executes code on usage threshold
  • custom usage reports using swiftui
  • new extension point allow you to choose what data to show
  • End- user privacy is assured

Lets start coding…

Create SwiftUI app and give to any name like — ScreenTime
then come to ScreenTimeApp file do some changes requirement and your app need to do on launch is require authorization,

import SwiftUI
import FamilyControls
@main
struct ScreenTimeApp: App {
let center = AuthorizationCenter.shared
//1 added next line here
var body: some Scene {
WindowGroup {
ZStack {
// 2 will do next logic - add
}
.onAppear {
Task {
do {
try await center.requestAuthorization(for: .individual)
} catch {
print("Failed to enroll Aniyah with error: \(error)")
}
}
}
}
}

I’m skipping Managed Settings here so i focus device activity for Device activity data usage

Now you need to add Device Activity Report Extension-
open file-New-Target selection and chose Device Activity Report Extension-
give some name any like — ReportExtension Name

Come back on your own file for device activity framework and added some line in your App file

import DeviceActivity

let center = AuthorizationCenter.shared
//1 added next line here
@State private var context: DeviceActivityReport.Context = .init(rawValue: "Total Activity")
@State private var filter = DeviceActivityFilter(
segment: .daily(
during: Calendar.current.dateInterval(
of: .day, for: .now
)!
),
users: .all,
devices: .init([.iPhone, .iPad])
)

Added these line in body view-

ZStack {
// 2 will do next logic - add
DeviceActivityReport(context, filter: filter)
}

Congrats you done….,
Now run your app and wait to see final result……………..

If you want to more details, make some changes in DeviceActivityReport (in generated extension)….

Bonus Point — wanna more details do some changes in DeviceActivityReport file — makeConfiguration function -

var res = ""
var list: [DeviceActivityModel] = []
for await d in data {
res += d.user.appleID!.debugDescription
res += d.lastUpdatedDate.description
for await a in d.activitySegments{
res += a.totalActivityDuration.formatted()
for await c in a.categories {
cat += (c.category.localizedDisplayName ?? "nil") + ","
for await ap in c.applications {
let appName = (ap.application.localizedDisplayName ?? "nil")
let bundle = (ap.application.bundleIdentifier ?? "nil")
let duration = ap.totalActivityDuration
let numberOfPickups = ap.numberOfPickups

// let app = AppDeviceActivity(id: bundle, displayName: appName, duration: duration, numberOfPickups: numberOfPickups)
let app = DeviceActivityModel(id: bundle, appName: appName, duration: duration, noOfPickups: numberOfPickups)
list.append(app)
}
}
}
}

Thank you !
Connect on Linkedin

Hope this is helpful for you 😄 chesssss…..

Complete Source- Code

NOTE: You can’t manipulate, store and share outside if device.

Reference — https://developer.apple.com/videos/play/wwdc2022/110336/

--

--