I have a request from a client that would like to create a solution where a central component is the ability to retrieve the users app usage. I can see that there are various api's todo that - one example is the Device Activity API. Another hope from the client is that this should be a web solution and not an app. I haven't quite been able to figure out if this is possible or not.
I have three questions:
Is it possible to retrieve app usage information through a REST api (or similar) outside an iOS app?
Is it possible to use the appleId as a kind of auth solution for a web application
Are there any similar APIs for older versions than iOS 15?
Device Activity
RSS for tagMonitor web and app usage through custom time windows and events.
Posts under Device Activity tag
82 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
How can I differentiate between multiple children in order to display screentime data to the user separately for a parent with multiple children? As far as I can see, the options are .children, and .all? I understand I can retrieve some info from within the extension so I can group data separately, but is there any way to reliably filter between different children from within my app, say using a DeviceActivityFilter?
Many thanks!
I am currently debugging an issue with DeviceActivityMonitor where the threshold is reached even though the target app (e.g. Instagram) is not being used actively.
I noticed that the device with the unexpected behavior had the instagram.com website opened in the Safari web browser (among hundreds of other tabs).
That tab was not actively used either (not in foreground, Safari app neither used).
However, I was wondering if it can happen that this website is contributing towards the threshold as well even though it is in background and not used?
Otherwise I cannot explain myself this strange behavior.
As per our code, we have the apps to be shielded whenever the threshold is reached. According to this use-case, our code in DeviceActivityExtension looks something like:
override func eventDidReachThreshold(_ event: DeviceActivityEvent.Name, activity: DeviceActivityName) {
super.eventDidReachThreshold(event, activity: activity)
defaults?.setValue(event.rawValue, forKey: "appLimitEventName")
defaults?.setValue(true, forKey: "appLimitReached")
defaults?.synchronize()
// using darwinNotificationCenter to trigger callback in the application
let darwinNotificationCenter = DarwinNotificationsManager.sharedInstance()
darwinNotificationCenter.postNotification(withName: "nextAppLimitInitiated")
// using Notifications to debug since print doesn't work
scheduleNotification(with: "interval threshold reached")
}
And in our application, we have the shielding logic in place,
init() {
let darwinNotificationCenter = DarwinNotificationsManager.sharedInstance()
darwinNotificationCenter.register(forNotificationName: "nextAppLimitInitiated"){
print("callback received")
let appLimitReached = self.defaults?.bool(forKey: "appLimitReached")
let appLimitEventName = self.defaults?.string(forKey: "appLimitEventName")
if appLimitReached ?? false, appLimitEventName != "" {
// this sends the notification when callback is received
self.scheduleNotification(with: "init start")
self.defaults?.setValue(false, forKey: "appLimitReached")
guard var dataArray = self.defaults?.array(forKey: "appLimitdataArray"), !dataArray.isEmpty else {
return
}
let appLimitData = dataArray.first as! NSDictionary
let appLimitKey = appLimitData["appLimitId"] as! String
let data = self.getSchedule(key: appLimitEventName ?? "")
if let appTokens = data?.applicationTokens {
for token in appTokens {
if !self.applicationTokens.contains(appTokens) {
self.applicationTokens.insert(token)
}
}
}
self.store.shield.applications = self.applicationTokens
self.store.shield.applicationCategories = ShieldSettings.ActivityCategoryPolicy.specific(self.categoryTokens, except: Set())
dataArray.removeFirst()
//dataArray.append(appLimitData)
self.defaults?.set(dataArray, forKey: "appLimitdataArray")
self.initiateMonitoring(initiateAgain: true)
self.scheduleNotification(with: "init end")
}
}
}
This works as expected for multiple App Limits but only when the device is connected to the Xcode. If we disconnect the device from Xcode/ stop application from Xcode/ try in release mode, the callback is not received from extension to the app/init block.
When the device is connected to Xcode, if the apps hit the threshold, they are shielded automatically. But if the device is disconnected/ app is in release mode, the apps are not shielded automatically even after the threshold is reached. It is shielded later only after opening our app once.
Please let me know if I'm doing anything wrong in receiving callback or in my shielding logic. If I need to place the shielding logic in the extension, please tell me how I can handle multiple appTokens.
I have two repeating DeviceActivitySchedules running in my app and noticed a super strange behavior:
whenever I modify Screen Time API permission settings (granting an additional app permission, or removing permission for a different app (completely different unrelated screen time apps from App Store)) all my DeviceActivitySchedules are cancelled and the intervalDidEnd callbacks are called
my permission is not modified in this scenario
this is a super strange and unexpected behavior
is anyone able to reproduce this?
for me this happens 100% of the times I do this
any known workarounds?
How can I count the number of pickups for a certain application?
The docs say that there is a struct ApplicationActivity within the DeviceActivityData:
https://developer.apple.com/documentation/deviceactivity/deviceactivitydata
Using this code I receive a warning in XCode:
Value of type 'DeviceActivityData' has no member 'applicationActivity'
struct ApplicationPickupCountReport: DeviceActivityReportScene {
// Define which context your scene will represent.
let context: DeviceActivityReport.Context = .applicationPickupCount
// Define the custom configuration and the resulting view for this report.
let content: (String) -> ApplicationPickupCountView
func makeConfiguration(representing data:
DeviceActivityResults<DeviceActivityData>) async -> String {
var totalPickups = 0
let totalPickups = await data.flatMap { $0.applicationActivity }.reduce(0, {
$0 + $1.numberOfPickups
})
return "\(totalPickups)"
}
}
Hello,
I am using a DeviceActivityEvent to limit access to an app after the user has spent x minutes on it.
Sometimes it can happen that a DeviceActivitySchedule spans from 11:55pm - 12:25am (just an example).
In these cases, I have noticed, the DeviceActivityMonitorExtension’s eventDidReachThreshold is not called after the user has reached the threshold time interval in the observed app.
I assume this is because we have transitioned over midnight and something weird happens to the DeviceActivitySchedule.
I’ve tried adding the day components to the DeviceActivitySchedule as well, but then it fails completely.
Any advice how to handle this?
I could, of course, create two separate DeviceActivitySchedules: one for before midnight, and one for after. But depending on the user’s real app usage that could lead to slightly different (and unexpected) behavior.
Hello!
I am using the eventDidReachThreshold callback in the DeviceActivityMonitor in order to shield a target app after the user has spend x amount of time on it (e.g. x = 5 minutes).
Many times this works fine, and I can trigger my shield after the specified threshold has been met.
However sometimes, when they leave the target app before the threshold has been reached, the eventDidReachThreshold callback gets called randomly while they are doing something else on their phone (e.g. using a different app, on the Home Screen, phone locked…).
From my perspective this does not make sense, since they are not actively spending time on the target app, and that time should not be counted towards the target app’s threshold.
And it is also very confusing for the users because they will then find a blocked target app even though they haven’t used their time budget completely.
This is not related to the intervalDidStart / intervalDidEnd callbacks, because they are not correlating with the timing of when the eventDidReachThreshold callback is called unexpectedly.
Any ideas what this could be related to?
There is a way to allow DeviceActivityMonitor to make UI changes?
Currently I have some code in the intervalDidStart and intervalDidEnd that updates Core Data entities, and it works correctly.
However, the changes are only reflected if the app is terminated and opened again.
Thus, if the app is opened and one of the methods inside the DeviceActivityMonitor is being called, the UI won't be updated.
PS: I am using @FetchRequest to retrieve the core data entities called BlockEntity, and I have a little circle in the UI which should indicate the status of the block: active/inactive, so it would be nice to update the color in real time when intervalDidStart or intervalDidEnd are called.
I am trying to understand how to approach 'x minute' pauses for a DeviceActivitySchedule.
For instance, I would like to let the user pause for 5 minutes from an active schedule (meaning un-shielding the apps and re-applying the shield after the 5 min has passed).
The only way that came to my mind was calling the following:
Calling .startMonitoring to start monitoring a new event with the same apps starting .now and ending .now + 5 minutes;
Calling in the intervalDidStart, store.shield.applications.subtract(apps) so that the apps are removed from the shield.
Calling in the intervalDidEnd, store.shield.applications = apps so that the apps are now shielded again.
The problem is that, from the Apple Developer Documentation:
The minimum interval length for monitoring device activity is fifteen minutes.
So the minimum pause I could offer to the user would be 15 minutes.
And that tells me this approach is most likely wrong, because all other Screen Time apps, like Opal, Jomo, AppBlock offer also 5 min pause.
Does anyone know / can think of a different and better approach?
I am developing a project with the Screen Time API, and I cannot understand why the methods inside DeviceActivityMonitor extension are not being called. Some points to note:
I do start by requesting authorization from the User (this is working as expected when opening the app)
Both the DeviceActivityMonitor Extension and the main app are under the same App Group
I have added Family Controls capability to both targets and have the developer account to use it.
I start by calling center.startMonitoring()
I have overridden the original methods.
Yet, when startMonitoring is called, there is no action that is supposed to be taken that is being taken, and as far as I can tell, the methods aren't even being called.
Here are some relevant snippets of code:
// EXT
class DeviceActivityMonitorExtension: DeviceActivityMonitor {
override func intervalDidStart(for activity: DeviceActivityName) {
super.intervalDidStart(for: activity)
let store = ManagedSettingsStore(named: .daily)
}
override func intervalDidEnd(for activity: DeviceActivityName) {
super.intervalDidEnd(for: activity)
let store = ManagedSettingsStore(named: .daily)
store.clearAllSettings()
}
override func eventDidReachThreshold(_ event: DeviceActivityEvent.Name, activity: DeviceActivityName) {
super.eventDidReachThreshold(event, activity: activity)
let store = ManagedSettingsStore(named: .daily)
let model = BeeFreeModel.shared
let applications = model.selectionToDiscourage.applicationTokens
let categories = model.selectionToDiscourage.categoryTokens
let webDomains = model.selectionToDiscourage.webDomainTokens
store.shield.applications = applications.isEmpty ?
nil : applications
store.shield.applicationCategories = categories.isEmpty ?
nil : ShieldSettings.ActivityCategoryPolicy.specific(categories)
store.shield.webDomains = webDomains.isEmpty ?
nil : webDomains
}
}
// APP
extension DeviceActivityName {
static let daily = Self("daily")
}
extension DeviceActivityEvent.Name {
static let discouraged = Self("discouraged")
}
let schedule = DeviceActivitySchedule(
intervalStart: DateComponents(hour: 0, minute: 0, second: 0),
intervalEnd: DateComponents(hour: 23, minute: 59, second: 59),
repeats: true
)
class BeeFreeSchedule {
static public func setSchedule() {
print("Setting schedule...")
print("Hour is: ", Calendar.current.dateComponents([.hour, .minute], from: Date()).hour!)
let events: [DeviceActivityEvent.Name: DeviceActivityEvent] = [
.discouraged: DeviceActivityEvent(
applications: BeeFreeModel.shared.selectionToDiscourage.applicationTokens,
categories: BeeFreeModel.shared.selectionToDiscourage.categoryTokens,
webDomains: BeeFreeModel.shared.selectionToDiscourage.webDomainTokens,
threshold: BeeFreeModel.shared.thresholdToDiscourage
)
]
let center = DeviceActivityCenter()
do {
print("Try to start monitoring...")
// Call startMonitoring with the activity name, schedule, and events
try center.startMonitoring(.daily, during: schedule, events: events)
} catch {
print("Error monitoring schedule: ", error)
}
}
}
// APP
class BeeFreeModel: ObservableObject {
// Import ManagedSettings to get access to the application shield restriction
let store = ManagedSettingsStore()
//@EnvironmentObject var store: ManagedSettingsStore
@Published var selectionToDiscourage: FamilyActivitySelection
@Published var thresholdToDiscourage: DateComponents
@Published var setOfApps: [String]
init() {
selectionToDiscourage = FamilyActivitySelection()
thresholdToDiscourage = DateComponents()
var setOfAppIdentifiers: Set<String?> = Set<String?>()
setOfApps = [String]()
if selectionToDiscourage.applicationTokens.isEmpty {}
else {
for application in BeeFreeModel.shared.selectionToDiscourage.applications {
setOfAppIdentifiers.insert(application.localizedDisplayName)
setOfApps = setOfAppIdentifiers.compactMap { $0 }.sorted()
}
}
}
class var shared: BeeFreeModel {
return _BeeFreeModel
}
func setSelection() {
let applications = BeeFreeModel.shared.selectionToDiscourage
}
func changeThreshold(threshold: DateComponents) {
thresholdToDiscourage = threshold
}
}
// APP
/// ...
Button("Select Apps to Discourage") {
isDiscouragedPresented = true
}
.familyActivityPicker(isPresented: $isDiscouragedPresented, selection: $model.selectionToDiscourage)
}
.onChange(of: model.selectionToDiscourage) {
BeeFreeModel.shared.setSelection()
}
// ...
// ...
Button("Save Time") {
saveTime()
let new_threshold = DateComponents(hour: savedTime?.hours,
minute: savedTime?.minutes,
second: savedTime?.seconds)
model.changeThreshold(threshold: new_threshold)
}
}
.onChange(of: model.thresholdToDiscourage) {
BeeFreeSchedule.setSchedule()
// ...
I believe I am using the latest stable version of Xcode (recently deleted and reinstalled). I'm really new to mobile development to Swift so any help is greatly appreciated.
Thank you! :)
Sometimes the app switcher doesn't show the correct shield configuration if there is like 7 or more apps in the app switcher.
if I clear all apps from app switcher cache and leave 4 apps, the correct shield configuration will show every time instantly when I change the shield configuration from my parent app and go back to the app switcher; but if there is like 7 or more apps in the app switcher, I can see some of the app's shield configurations don't update instantly and I have to scroll past the app in the app switcher and go back to for it to update to the correct shield if it does at all.
This is misleading to users and in the case of my app detrimental to one of its core functionalities
override func configuration(shielding application: Application) -> ShieldConfiguration {
let sharedUserDefaults = UserDefaults(suiteName: SharedUserDefaults.suiteName.rawValue)!
let isPaidOn = sharedUserDefaults.bool(forKey: SharedUserDefaults.paidSwitchKey.rawValue)
if isPaidOn == true {
return ShieldConfig.paid
} else {
return ShieldConfig.free
}
}
Starting with iOS 17.4, the DeviceActivityEvent initializer includes a new parameter named includesPastActivity.
The default value for this parameter is set to false, whereas device activity events have behaved as though this parameter were set to true up until now.
This breaking change is a MAJOR ISSUE for developers who used device activity events in their apps before iOS 17.4 because their apps might not work the way they intended after the update. They'll have to release new app versions that specifically set includesPastActivityto true.
In my opinion, the default value for includesPastActivity should be true to avoid disrupting events scheduled on older versions of iOS.
I have filed an enhancement report (FB13573556) about this. I really hope this is changed before the official iOS 17.4 release.
App with DeviceActivityReport extension either won't install on device or won't upload to TestFlight
We have integrated DeviceActivityReport to our Family Controll app. All capabilities are added. We built the app and tested it, but when we want to upload to TestFlight the following error happened (see attached image)
When we are adding NSExtensionMainStoryboard or NSExtensionPrincipalClass the following error appears during installation process.
DeviceReport.appex with id com.example.example.DeviceReport defines either an NSExtensionMainStoryboard or NSExtensionPrincipalClass key, which is not allowed for the extension point com.apple.deviceactivityui.report-extension
The device activity monitor extension sometimes fails to launch when a schedule starts/ends or when an event threshold is reached. This issue may persist for several minutes or hours once it begins.
Currently, the only known workarounds are:
Restarting the phone.
Waiting for an indeterminate period, ranging from a few minutes to hours, after which the device activity extension may or may not launch.
I've filed a new bug report (FB13556935), which includes a sysdiagnose and precise timestamps of when the issue was reproduced. By looking at the errors, it appears the system is failing to launch the extension because it's looking at an invalid path:
Service could not initialize: access(/private/var/containers/Bundle/Application/C6598B47-8977-447C-870B-4D21BDE8ACF9/Jomo.app/PlugIns/JomoDeviceActivityMonitor.appex/JomoDeviceActivityMonitor, X_OK) failed with errno 2 - No such file or directory, error 0x6f - Invalid or missing Program/ProgramArguments
[u 8104FF60-5C49-45BD-8AFB-97BE88488134:m (null)] [()] Failed to start plugin; pkd returned an error: Error Domain=PlugInKit Code=4 "RBSLaunchRequest error trying to launch plugin com.jomo.Jomo.JomoDeviceActivityMonitor(8104FF60-5C49-45BD-8AFB-97BE88488134): Error Domain=RBSRequestErrorDomain Code=5 "Launch failed." UserInfo={NSLocalizedFailureReason=Launch failed., NSUnderlyingError=0x5dd8273e0 {Error Domain=NSPOSIXErrorDomain Code=111 "Unknown error: 111" UserInfo={NSLocalizedDescription=Launchd job spawn failed}}}" UserInfo={NSLocalizedDescription=RBSLaunchRequest error trying to launch plugin com.jomo.Jomo.JomoDeviceActivityMonitor(8104FF60-5C49-45BD-8AFB-97BE88488134): Error Domain=RBSRequestErrorDomain Code=5 "Launch failed." UserInfo={NSLocalizedFailureReason=Launch failed., NSUnderlyingError=0x5dd8273e0 {Error Domain=NSPOSIXErrorDomain Code=111 "Unknown error: 111" UserInfo={NSLocalizedDescription=Launchd job spawn failed}}}}
This bug is CRITICAL for all apps relying on the Device Activity framework. It likely explains a range of issues reported since the framework's release. Specifically, due to this bug, end users may encounter problems such as apps not unblocking at the end of a schedule, apps not blocking at the start of a schedule, and time limits not being updated, among others.
Having tried to gain access to the total daily screen time API using the text/code from the below thread, we were unable to get any data access. Has anyone else had the same issue and any resolutions? Not looking for application limits, just statistics to inform user through our 3rd party application.
Thanks for any guidance around areas we can attempt
https://developer.apple.com/documentation/deviceactivity/deviceactivitydata/activitysegment
I am able to correctly select and store the tokens of apps and categories my users will block, but I am unable to pre-populate Picker whenever the app is rebooted with previously selected and stored tokens. Below is the selection variable I am passing to the picker...
var selectionsToBlock = FamilyActivitySelection() {
willSet {
saveSelection(selection: newValue)
blockersSelected = true
}
}
Is there any way I can provide my existing blockers (shown below) so that the user can easily edit their list of restricted apps from the Picker?
func savedBlockers() -> FamilyActivitySelection? {
let defaults = UserDefaults.standard
guard let data = defaults.data(forKey: userDefaultsKey) else {
return nil
}
return try? decoder.decode(
FamilyActivitySelection.self,
from: data
)
}
I'm developing a mental wellness app, and I need to get user's screentime and app usage data and send it to my flask backend for analytics. Is it possible on iphone right now?
I've looked into ScreenTime and DeviceActivity frameworks, but they're really poor in terms of examples and documentation, so I'm not sure if this is actually possible or not.
I use App Groups to share UserDefaults data between my host app and DeviceActivityMonitor extension.
On iOS 17, it appears that reading @AppStorage variables are causing my DeviceActivityMonitor extension callback functions to crash. Weirdly, writing values is okay.
I see this in the extension logs:
Couldn't read values in CFPrefsPlistSource<0x10fe251d0> (Domain: GROUP_NAME, User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null), Contents Need Refresh: Yes): Using kCFPreferencesAnyUser with a container is only allowed for System Containers, detaching from cfprefsd
However, through searching this log message on the internet and the fact that it also appears in my host app logs without crashing, this seems to be a warning - possibly indicating an issue but also a possible red herring.
But the fact remains that when I don't read UserDefaults values or variables decorated with @AppStorage in the DeviceActivityMonitor extension, everything works fine.
Are UserDefaults, and specifically @AppStorage decorators supported in the DeviceActivityMonitor extension?
Where is Screen Time history data stored on iOS 15.8 (iPhone 6s)?