Programmatically get application usage from Screen Time in iOS 12
Asked Answered
I

4

11

I am working on one project and I want to get other application usage time. apple has launched iOS 12 and provides the new feature Screen Time. I want to know is there any way or API provided by Apple to get data from it.

Insane answered 24/9, 2018 at 4:28 Comment(1)
After WWDC22, I guess now you can do this! developer.apple.com/videos/play/wwdc2022/110336Squatter
D
11

No, on iOS each app runs in its own sandbox and cannot see data from other apps, even apps from the same publisher. An app can access special shared data, like photos from the album or contact data, but it is not possible for an app to directly access files written by other apps.

ScreenTime application is on another sandbox than your app. Unless apple provide API we can't access the same.


Update

Since apple have added screen time app into ios12 simulator. There could be a chance they provide the API's soon.

Diapedesis answered 24/9, 2018 at 4:59 Comment(3)
Has there been any update regarding Apple Screen Time APIKeenakeenan
@juske no updates from apple on Apple Screen Time APIsDiapedesis
3 years later, is it still the same case?Anniceannie
T
2

On iOS 15 Apple has added an API, see here: https://developer.apple.com/documentation/FamilyControls

FamilyControls

Authorize your app to provide parental controls on a child’s device. To authorize your parental controls app, use a shared AuthorizationCenter instance. You can authorize parental controls only on a device that is part of a Family Sharing group and signed into a child’s iCloud account.

You must add the Family Controls capability to your app.

Tough answered 8/6, 2021 at 11:12 Comment(0)
S
0

This seems to provide functionality to parental control apps only. In other words, it cannot be used by other time-tracking or productivity apps at the moment.

Schooling answered 12/10, 2021 at 21:22 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Partisan
C
0

Yes, it is possible to get screen time data using the Screen Time API, which comprises three frameworks:

  1. Parental Controls
  2. Device Activity
  3. Managed Settings

With these three frameworks, you can retrieve screen time information.

To integrate this in an Xcode iOS project, go to File -> New -> Target -> Device Activity Report Extension. This will add a Device Activity Report Extension target to your project, automatically creating three files:

  1. ScreenTimeReport.swift
  2. TotalActivityReport.swift
  3. TotalActivityView.swift

To display the overall screen time of the user, add the following code to any view file. This will prompt the user for permission and, upon approval, display their screen time.

struct ExampleView: View {
    @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])
    )
    
    var body: some View {
        DeviceActivityReport(context, filter: filter)
    }
}

This code will call the TotalActivityView.swift file.

Hurray! The screen time will be displayed.

Clandestine answered 16/5 at 23:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.