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.
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.
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.
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.
Yes, it is possible to get screen time data using the Screen Time API, which comprises three frameworks:
- Parental Controls
- Device Activity
- 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:
- ScreenTimeReport.swift
- TotalActivityReport.swift
- 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.
© 2022 - 2024 — McMap. All rights reserved.