Search results for

DeviceActivityMonitor

127 results found

Post

Replies

Boosts

Views

Activity

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
Setting values in UserDefaults in DeviceActivityMonitor extension
I understand that the DeviceActivityMonitor extension is designed to be very lightweight, and the system terminates the extension as soon as its callback functions return. However, I want to save values to UserDefaults inside the extension's callback functions. This creates concurrency issues for my app because the documentation for UserDefaults states that When you set a default value, it’s changed synchronously within your process, and asynchronously to persistent storage and other processes. In order to guarantee that these values are persisted before the extension terminates my app, I want to call UserDefaults.synchronize(), but its documentations states that it's unnecessary and shouldn't be used. Furthermore, it's listed under Legacy but not marked deprecated. Is synchronize() the recommended way to solve my concurrency problem? Or could there be a better way to wait for storage synchronization before returning from a synchronous function?
5
0
2.1k
Apr ’23
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
Trigger Action After eventDidReachThreshold
I am trying to configure my app to monitor device screen time and shield apps after a certain amount of screen time is reached. Here's the scheduled I created: let schedule = DeviceActivitySchedule( // I've set my schedule to start and end at midnight intervalStart: DateComponents(hour: 0, minute: 0), intervalEnd: DateComponents(hour: 23, minute: 59), // I've also set the schedule to repeat repeats: true ) class MySchedule { var item: NoteItem init(item: NoteItem) { self.item = item } public func setSchedule() { print(Setting schedule...) print(Hour is: , Calendar.current.dateComponents([.hour, .minute], from: Date()).hour!) var threshold = DateComponents(hour: item.hour, minute: item.min) let events: [DeviceActivityEvent.Name: DeviceActivityEvent] = [ .discouraged: DeviceActivityEvent( applications: NotificationsModel.shared.selectionToDiscourage.applicationTokens, threshold: threshold ) ] let center = DeviceActivityCenter() do { print(Try to start monitoring...) try center.startMonitoring(.daily, during: sc
1
0
972
Apr ’23
Reply to Trigger Action After eventDidReachThreshold
Yes, you can create a DeviceActivityMonitor app extension and override its eventDidReachThreshold function. The system will invoke your principal class's implementation of that function whenever an event's threshold is reached. You can create one of these extensions in Xcode by clicking File > New > Target > Device Activity Monitor Extension. Xcode will autogenerate a properly configured extension target with a simple implementation of DeviceActivityMonitor that you can then modify to shield apps whenever a threshold is reached.
Topic: App & System Services SubTopic: General Tags:
Apr ’23
ManagedSettings can't shield Messages and other system apps
When using ManagedSettingsStore to shield apps, no system apps are shielded even when specifying all application categories. Here is my code: managedSettings.shield.applicationCategories = .all() Even when using the FamilyActivityPicker and selecting All Apps & Categories system apps like Messages do not get shielded. managedSettings.shield.applicationCategories = .specific(selectedCategories) I find this strange, since Messages exists inside the Social category, and is tracked fine using DeviceActivityMonitor. Why can't it be shielded using app categories? I'd like to be able to shield all apps, including Messages, without having the user to specifically select the apps using FamilyActivityPicker. Is that possible?
1
0
1.4k
May ’23
DeviceActivityMonitorExtension functions not being called
I'm having trouble with my DeviceActivityMonitorExtension. The intervalDidStart function is not being called when the scheduler is created. Does anyone have an idea why this is? let schedule = DeviceActivitySchedule( intervalStart: DateComponents(hour: 15, minute: 23), intervalEnd: DateComponents(hour: 16, minute: 55), repeats: true ) class MySchedule { static public func setSchedule() { let center = DeviceActivityCenter() center.stopMonitoring([.daily]) do { try center.startMonitoring(.daily, during: schedule) } catch { print(Error monitoring schedule: , error) } } } class DeviceActivityMonitorExtension: DeviceActivityMonitor { override func intervalDidStart(for activity: DeviceActivityName) { super.intervalDidStart(for: activity) SelectedApps.shared.setRestrictions() } private let _SelectedApps = SelectedApps() class SelectedApps: ObservableObject{ @Published var selection: FamilyActivitySelection let store = ManagedSettingsStore() init() { if let savedSelection = UserDefaults.standard.object(forKe
3
0
1.1k
Jul ’23
DeviceActivityMonitor Extension Not Working
I added the extension via targets. And I added code in the intervalDidStart() override to print and send a notification. But it never seems to run. Am I supposed to do any other steps after adding the extension? // Make sure that your class name matches the NSExtensionPrincipalClass in your Info.plist. class DeviceActivityMonitorExtension: DeviceActivityMonitor { func getSelectionFromUserDefaults() -> FamilyActivitySelection { let defaults = UserDefaults.standard if let encodedData = defaults.data(forKey: ScreenTimeSelection) { let decoder = PropertyListDecoder() if let selection = try? decoder.decode(FamilyActivitySelection.self, from: encodedData) { return selection } } return FamilyActivitySelection() } override func intervalDidStart(for activity: DeviceActivityName) { super.intervalDidStart(for: activity) print(HERE!) let content = UNMutableNotificationContent() content.title = Feed the cat content.subtitle = It looks hungry content.sound = UNNotificationSound.default // show this notification
2
0
1k
Jul ’23
Memory limit for Device Activity Monitor Extension
I am trying to develop an application that can enforce Screen Time restrictions on the target device, however I get stuck at the part where I have scheduled callbacks for my class that implements the extension for DeviceActivityMonitor. For debugging, I first attach to the app extension target process via Debug > Attach to Process > Process Name. But after scheduling monitoring that receives a callback immediately, the process exits with the following error: Thread 1: EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory limit exceeded) (limit=6 MB) Is the maximum allowed memory for Device Activity Monitor extensions really just 6 MB, or am I doing something wrong? If it is indeed the case, is there any way to increase it? 6 MB seems quite restrictive to me.
1
0
1.9k
Aug ’23
Schedule Task (Screen Time API) on iOS in background mode
I have developed a Flutter application to lock selected apps using the Screen Time API (iOS). My app has been registered in Family Control and currently successfully executes app restrictions in both emulators and devices through TestFlight. I intend to incorporate a countdown timer feature to ensure that the app restriction function is activated when the timer stops (e.g 15 minutes after function called). However, the issue I'm facing is that on iOS, when the app goes into the background mode, all processes, including the countdown, halt, rendering the restriction function unusable. I have attempted various mechanisms, such as: DeviceActivityMonitor extension => Not triggered even though DeviceActivitySchedule is set. Cron => Stops when in background mode. Local notification => Only triggered when the user interacts with the notification, not automatically execute function in background Is there a solution to address this matter? Here is my snippet code, startAppRestrictions() is already wo
1
0
1.7k
Aug ’23
Irrecoverable Crash in Extension
We are seeing cases where DeviceActivityMonitoring extension is crashing permanently. This is due to a now-fixed bug that was causing a stack overflow. On devices where the extension crashed, it appears permanently unable to recover - even weeks after the crash and after multiple restarts of the device. It looks like recovery is being attempted, as the same four NSLog activities are emitted roughly every second that the phone is active. Logs: Process: DeviceActivityMonitoring Activity: beginning extension request Message: NSExtensionPrincipalClass `` does not conform to NSExtensionRequestHandling protocol! Process: DeviceActivityMonitoring Activity: Loading Preferences From User Session CFPrefsD Message: Process: DeviceActivityMonitoring Activity: container_system_group_path_for_identifier Message: Requesting container lookup; class = 13, identifier = , group_identifier = , create = 1, temp = 0, euid = 501, uid = 501 container_query_get_single_result: success container_syst
1
0
954
Aug ’23
DeviceActivityMonitor not working on development since I've had my Family Controls (Distribution) entitlement approved
I recently got approved for the Family Controls (Distribution) entitlement, and since then it seems that I cannot use the DeviceActivityMonitor extension on the dev environment anymore. I've tried attaching a debugger to the DeviceActivityMonitor process but it's never called so does not attach, and I can't see why it is not attaching. I've tried reverted back to old versions which I know definitely worked, and it's still not working... However, the DeviceActivityReport extension seems to be working fine. Any help or advice on how I can actually debug this would be greatly appreciated!
1
0
947
Aug ’23
Reply to Schedule Task (Screen Time API) on iOS in background mode
Fixed by recreating Target Extension & changing from print log to show from local notification, somehow Print log from the target extension is not showing on XCode even though I already do Debug > Attach to the Process // // DeviceActivityMonitorExtension.swift // NewMonitor // // Created by Mochammad Yusuf Fachroni on 18/08/23. // import DeviceActivity import UserNotifications import ManagedSettings // Optionally override any of the functions below. // Make sure that your class name matches the NSExtensionPrincipalClass in your Info.plist. class DeviceActivityMonitorExtension: DeviceActivityMonitor { func showLocalNotification(title: String, desc: String) { let content = UNMutableNotificationContent() content.title = title content.body = desc content.sound = .default let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false) let request = UNNotificationRequest(identifier: localNotification, content: content, trigger: trigger) UNUserNotificationCenter.current().add(reques
Topic: App & System Services SubTopic: General Tags:
Aug ’23
Stability issues with ManagedSettings on iOS 16.6? Lots of new crashes
Hello, I am curious if someone else also noticed this. We have started getting reports about our app not working correctly (mostly related to the Device Activity monitor that runs in the background, https://developer.apple.com/documentation/deviceactivity/deviceactivitymonitor). Upon checking the Xcode Organizer I can see somewhat significant amount of crashes that mostly appear to happen on iOS 16.6 - it is possible that our users don't wait with updating iOS but still looks suspicious. The crashes are related to ManagedSettings calls outside our own code. We haven't changes this code in a while so this coupled with the fact that these crashes happen deep in the ManagedSettings framework leads me to believe there is some other issue.
3
0
1.1k
Aug ’23
DeviceActivityMonitorExtension - intervalDidStart doesn't get called
Hello, I have an app that you can select apps and then start monitoring. When I restrict the apps by button click, and monitor the activity, the scheduled time works and intervalDidEnd cancels shielding apps. But when I schedule shielding apps, intervalDidStart doesn't start shielding. What am I missing here? I have already added FamilyControls capability. import SwiftUI @main struct TestingScreenTimeAPIApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { ContentView() } } } import SwiftUI struct ContentView: View { @StateObject var model = MyModel.shared @State var isPresented = false var body: some View { VStack { Button(Select Apps) { isPresented = true } .familyActivityPicker(isPresented: $isPresented, selection: $model.selectionToDiscourage) Button(Start monitoring) { model.startMonitoring() } .padding() } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } import Foundation import FamilyControls
3
0
1.1k
Aug ’23