Search results for

DeviceActivityMonitor

127 results found

Post

Replies

Boosts

Views

Activity

Reply to Setting values in UserDefaults in DeviceActivityMonitor extension
I'm storing timestamps of certain screen time thresholds with DeviceActivityMonitor. Whether or not this is its intended use, something is still off. DeviceActivityMonitor is part of an app extension that is invoked and terminated by the system. Once its functions return, the extension is immediately terminated, meaning there may be data in UserDefaults that never gets persisted. The also docs don't say it's a no-op just that it's unnecessary - but maybe it's necessary inside an app extension?
Topic: App & System Services SubTopic: General Tags:
Apr ’23
Reply to Designate principal class
Use Call directory Extension Template and create an extension. than create a sub class of DeviceActivityMonitor and change that class as a principle class in the info.plist // MyDeviceActivityMonitor import Foundation import DeviceActivity import ManagedSettings class MyDeviceActivityMonitor: DeviceActivityMonitor{ override func intervalDidStart(for activity: DeviceActivityName) { super.intervalDidStart(for: activity) } override func intervalDidEnd(for activity: DeviceActivityName) { super.intervalDidEnd(for: activity) } override func eventDidReachThreshold(_ event:DeviceActivityEvent.Name,activity:DeviceActivityName){ super.eventDidReachThreshold(event, activity: activity) } } NSExtension NSExtensionPointIdentifier com.apple.deviceactivity.monitor-extension NSExtensionPrincipalClass $(PRODUCT_MODULE_NAME).MyDeviceActivityMonitor
Topic: Business & Education SubTopic: General Tags:
Aug ’21
Issue with DeviceActivityMonitor - eventDidReachThreshold Callback Not Triggering Properly
Hello, I'm currently experiencing an issue with the DeviceActivityMonitor extension in my code, specifically with the eventDidReachThreshold callback. I'm hoping to get some insights into why this problem occurs and how to resolve it. Problem: Issue 1: The eventDidReachThreshold callback is not triggering as expected. It appears that the callback is not being invoked when the threshold is reached. Issue 2: After a few seconds, the eventDidReachThreshold callback starts to trigger multiple times. This unexpected behavior is causing problems in my code, as it results in incorrect actions being taken. iOS version: iOS16.7.2 and iOS17.1 Xcode version: 15.0.1 Swift version: 5.9 Here is my code to start the monitoring: func startMonitoring() { var startTime : DateComponents = DateComponents(hour: 0, minute: 0) let endTime : DateComponents = DateComponents(hour: 23, minute: 59) /// Creates the schedule for the activity, specifying the start and end times, and setting it to repeat. let schedule = DeviceActiv
1
0
1k
Nov ’23
Reply to Screen Time API is completely UNRELIABLE!
Hi @Ivan018 . To check if DeviceActivityMonitor delegates work, I created a singleton class, marked the target to both Project and DeviceActivityMonitor. This way I am able to access the selected apps from family activity picker in the monitor extension. I am shielding the apps in intervalDidEnd. You can try the same in intervalDidStart override func intervalDidEnd(for activity: DeviceActivityName) { super.intervalDidEnd(for: activity) // Handle the end of the interval. /// Once interval ends restrict the app. let model = DataModel.shared let selection = model.getScheduledSelection() let applications = selection.applicationTokens store.shield.applications = applications.isEmpty ? nil : applications }
Topic: App & System Services SubTopic: General Tags:
May ’24
Screen Time API: How to map bundleIdentifier to ApplicationToken for DeviceActivityMonitor when FamilyActivitySelection.Application.bundleIdentifier is nil?
I'm using FamilyActivityPicker to get consent for app/category management, which returns a FamilyActivitySelection object. I serialize this FamilyActivitySelection object (just applicationTokens and categoryTokens) and pass it to my DeviceActivityMonitor extension via App Group UserDefaults. I am using the JSON encoder/decoder over PropertyList (though both seem to exhibit the same behavior). After inspecting the FamilyActivitySelection object immediately after it's returned by FamilyActivityPicker in the main app, the application.bundleIdentifier property is consistently nil for every Application object within selection.applications. Similarly, category.localizedDisplayName is nil for ActivityCategory objects. This happens whether Select All Apps is used or if apps/categories are selected individually. I understand that this is the intended behavior due to Apple's user privacy policies. I read on another post that my app can be provided with bundle identifiers and app names within Shield Configurati
0
0
194
May ’25
Reply to Module 'DeviceActivity' not found
I was also able to reproduce this by creating a new barebones project, with adding only two files: MyObjectiveC.m #import MyTestProject-Swift.h and MySwift.swift import DeviceActivity import ManagedSettings import Foundation @objc(MyMonitor) class MyMonitor: DeviceActivityMonitor { }
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’22
DeviceActivityMonitor - intervalDidStart() not able to restrict apps/categories/webCategories
I've followed along with the Screen Time API demos (https://developer.apple.com/videos/play/wwdc2021/10123/) Also followed along with this guide, which is essentially the same: https://www.folio3.com/mobile/blog/screentime-api-ios/ I'm able to restrict access to apps/categories with the FamilyActivityPicker and FamilyActivitySelection. I can set a DeviceActivitySchedule, and then use DeviceActivityCenter to start monitoring it. I can tell that the schedule is working, because MyMonitor:intervalDidStart() does get called, and it works except for the restricting of apps/categories/webCategories. It's as if MyModel does not have any values set from within MyMonitor. I've confirmed that MyModel does have the correct FamilyActivitySelection apps etc, everywhere else in my Target, before and after the MyMonitor:intervalDidStart() gets called. MyMonitor is in a separate target called MonitorExtension, that I created using the Device Activity Monitor Extension template. But I made sure to set the Target Membership of
8
0
3k
Dec ’22
FamilyControls DeviceActivityMonitor extension can't change shields
I've been working on a small app using the new FamilyControls API, and I can't get the extension to make any visible changes to shields (or anything else). I can get the main app to authorize and set and remove shields reliably. I can also get the extension to run reliably in the background on a schedule (confirmed with Console). But even if I try only setting the restricted apps to nil or the empty set the apps remain shielded until I run the same code from the main app. I do have the Family Controls entitlement set on both the app and the extension, and I've tried having it off on the extension. I have tried putting both of them into an App Group to no effect. I've wrapped up the most basic working example I can think of into a project: https://github.com/dpowers/ScreenPact Any ideas about what stupid thing I'm missing?
5
0
2.6k
Nov ’21
Reply to Module 'DeviceActivity' not found
I was also able to reproduce this by creating a new barebones project, with adding only two files: MyObjectiveC.m #import MyTestProject-Swift.h and MySwift.swift import DeviceActivity import ManagedSettings import Foundation @objc(MyMonitor) class MyMonitor: DeviceActivityMonitor { }
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’22
Reply to How does child can unblock apps for some period of time using Screen Time API?
what your code actually says is: after 30 seconds of total usage of the application that you selected (applications.applicationTokens) the eventDidReachThreshold of the DeviceActivityMonitor extension will be called. handle your event .encouraged inside the eventDidReachThreshold and do there what ever you like. i think for you case if you want to limit some apps for a period of time just make a scheduled event that start from the current time you tapped an OK button (or some other action) to current time + limitation time of tour choice. and on your DeviceActivityMonitor extension you should handle the blocking and unblocking on the intervalDidStart and intervalDidEnd the your code should look something like this: do { let schedule = DeviceActivitySchedule(intervalStart: start, intervalEnd: end, repeats: false, warningTime: nil) try center.startMonitoring(.block, during: schedule) } catch { print(error: + error.localizedDescription) }
Topic: App & System Services SubTopic: General Tags:
Jan ’22
DeviceActivityMonitor eventDidReachThreshold triggering every time in same second as intervalDidStart gets called
Hello, I am trying to make use of Screentime API in my app, I have issue with the DeviceActivityMonitor extension. I have schedule DeviceActivitySchedule which I set like this: let schedule = DeviceActivitySchedule( intervalStart: DateComponents(hour: 00, minute: 00), intervalEnd: DateComponents(hour: 23, minute: 59), repeats: false ) and DeviceActivityEvent which I set like this: let dateComponent = DateComponents(minute: 1) var events: [DeviceActivityEvent.Name: DeviceActivityEvent] = [ .encouraged: DeviceActivityEvent(threshold: dateComponent) ] The issue is that every time I start monitoring, by calling this piece of code from the app: do { print(Try start monitoring...) try center.startMonitoring(.daily, during: schedule, events: events) } catch { print(Error: , error) } I catch in the extension that the event intervalDidStart is called, but in the same second I get called eventDidReachThreshold. What could be done wrong? Is event set properly? I was trying to set event with different Datecompon
3
0
1.7k
Apr ’23
Reply to Module 'DeviceActivity' not found
The issue here is that, in most languages, including Objective-C, a class’s super class is part of the class’s public API. In your example the MyMonitor class has a public use of DeviceActivityMonitor, which means that Swift has to include the framework that exports that class, DeviceActivity, in the generated MyTestProject-Swift.h header. However, DeviceActivity is a Swift-only framework, and so everything falls apart. One way around this is to add another layer of wrapping: import Foundation import DeviceActivity @objc(ObjCMonitor) class ObjCMonitor: NSObject { override init() { self.sm = SwiftMonitor() } private let sm: SwiftMonitor } private class SwiftMonitor: DeviceActivityMonitor { } Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = eskimo + 1 + @ + apple.com
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’22
Reply to Module 'DeviceActivity' not found
As Quinn suggested, you can add another layer of wrapping around your MyMonitor class. However, I don't see why you would need to use a DeviceActivityMonitor subclass in Objective-C code. The superclass is intended to be subclassed and used only in the context of your Device Activity monitor extension as the extension's principal class.
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’22
Reply to DeviceActivityMonitor - Callbacks not trigger
Hi, I've faced a unexpected behavior in my screen time app (AppStops) when using structured concurrency tools in non-isolated DeviceActivityMonitor class. This changes may solve your problems. Remove @MainActor from AppUsageManager Remove Task from your codes. I recommend you to use non-isolated types and methods for DeviceActivityCenter. I hope this helps.
Topic: App & System Services SubTopic: General Tags:
Jan ’25
Reply to Interact with ManagedSettingsStore from DeviceActivityMonitor
Starting in iOS 16, ManagedSettingsStores will share the same settings in the DeviceActivityMonitor extension as the host application. Meaning if you set a setting (like shield.applications) in ManagedSettingsStore() in the app, you will see the same setting in ManagedSettingsStore() in the extension. So, for example this will shield apps in the host application, and clear those same shields once the activity ends. App: func shieldApplications() { let selections = FamilyActivitySelection(includeEntireCategory: true) /* Get Application Tokens from FamilyActivityPicker */ let store = ManagedSettingsStore() store.shield.applications = selections.applicationTokens } Extension: class MyDeviceActivityMonitor: DeviceActivityMonitor { let store = ManagedSettingsStore() public override func intervalDidEnd(for activity: DeviceActivityName) { super.intervalDidEnd(for: activity) store.shield.applications = nil } } If you do not want this behavior, please use the ManagedSettingsStore(named:) initializer
Topic: App & System Services SubTopic: General Tags:
Mar ’23