Search results for

DeviceActivityMonitor

127 results found

Post

Replies

Boosts

Views

Activity

DeviceActivityMonitor unreliable with iOS 17 - any other ways to schedule tasks?
We have an app that uses the Screen Time APIs to block certain apps set by the user on a schedule: We use ManagedSettings to shield selected apps We use DeviceActivityMonitor to shield the apps automatically on a schedule set by the user. The shielding starts during the intervalDidStart callback function and ends during the intervalDidEnd callback function We are getting reports from the majority of our iOS 17 users that the app blocking schedules no longer work on iOS 17. We have tested this on our own iOS 17 devices and reproduced the behavior. But the feature still works consistently on iOS 16 devices. The app is still built using Xcode 14 instead of Xcode 15 due to another issue - the DeviceActivityReport is blank for all iOS 16 users when built in Xcode 15 (link to issue on the developer forums: https://developer.apple.com/forums/thread/735915). When testing with Xcode 15 builds, the bug appears to improve - however it still occurs intermittently. Are there any other mechanisms to run tasks on r
2
0
1.2k
Dec ’23
Reducing Memory Usage in DeviceActivityMonitor: Help Needed
Hi everyone, I am a beginner in Swift and I am currently using DeviceActivityMonitor to develop an app for parents to control their children's app usage time. During the development process, I found that the DeviceActivityMonitor has a 6MB memory limit in the background, and my app exceeds this limit after running for a long time. I would like to know how to reduce the memory usage of DeviceActivityMonitor in the background, or where I can check the memory usage of DeviceActivityMonitor and see which variables are consuming memory. Additionally, I want to know if a new instance of the DeviceActivityMonitor class is created every time it is called? Thank you for your help!
1
0
663
Aug ’24
Reply to Best way to pause DeviceActivitySchedule
I finally found a suitable solution, though it may not be the best approach. I'm storing the selection in shared UserDefaults with the Schedule ID as the key. This way, I can always retrieve the selected apps when I have the Schedule ID. This solved the problem of not having access to the apps that should be restricted. The other issue is actually pausing the monitoring, for which this solution is quite elegant: When the pausing action starts, all restrictions for this activity should be cleared, and the activity should no longer be monitored. Start a new activity with the identifier pause+(oldID) using the same selection and schedule times. The new schedule should contain a proper warningTime. Learn more about it here (it's not as simple as you might think): Apple Developer Documentation. In your DeviceActivityMonitor, your intervalDidStart(for activity:) function will now be called. But since the new schedule is started with the ID pause+(oldID), you can react to the name of the activity. You can n
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’24
Best way to pause DeviceActivitySchedule
Hi everyone, I'm currently working with the Screen Time API, specifically trying to figure out the best way to pause an active, repeating schedule without disrupting future schedules. Here's the scenario: I have a repeating schedule set from 10:00 AM to 8:00 PM daily. If I need to pause the schedule temporarily, my current approach involves stopping monitoring, clearing all restrictions, and then setting a new schedule for the remaining time after the pause. However, this method cancels the repeating schedule entirely, which causes issues when the schedule is supposed to restart the next day at 10:00 AM. Instead, it starts after the pause time, which isn’t what I want. Challenges I'm Facing: Maintaining Repeating Schedules: How can I pause the schedule in a way that ensures it resumes correctly the next day at the intended time (10:00 AM)? DeviceActivityMonitor Logic: Is there a way to deactivate and reactivate the schedule through the DeviceActivityMonitor without fully stopping the monitor
1
0
916
Aug ’24
Ending Live Activities When Timer Expires
Hi everyone, I'm working on an app that uses the Screen Time API, and I'm encountering an issue with Live Activities. The app runs a timer (e.g., 10 minutes), after which the intervalDidEnd method in DeviceActivityMonitor is triggered. To improve the user experience, I've implemented Live Activities to display the remaining time. However, I'm having trouble stopping the Live Activity when the timer expires and intervalDidEnd is called. It seems that the Screen Time extensions cannot detect or interact with active Live Activities, even though both share the same App Group. My Question: Since the DeviceActivityMonitor extension does not seem to be able to detect Live Activities, does anyone know if there’s a way to end a Live Activity when the timer expires without relying on a server? Using Apple’s Push Notification service feels excessive given the lightweight nature of the app, which doesn’t use server-side components. Any insights or suggestions would be greatly appreciated! Thanks!
1
0
1.1k
Aug ’24
UserDefaults and @AppStorage causing DeviceActivityMonitor to crash on iOS 17
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 spe
2
0
1.4k
Dec ’23
The DeviceActivityMonitor did not trigger a callback
The listening callbacks were not triggered for intervalDidStart and intervalDidEnd after successfully starting center.startMonitoring when I attempted to set a schedule with DeviceActivitySchedule at 20-minute intervals Is there anyone who can assist me? Thank you. Below you will find my code. let intervalLengthInSeconds = 20 * 60 let intervalEnd = Date(timeIntervalSinceNow: TimeInterval(intervalLengthInSeconds)) let intervalStart = Date() let schedule = DeviceActivitySchedule(intervalStart: Calendar.current.dateComponents([.hour, .minute], from: intervalStart), intervalEnd: Calendar.current.dateComponents([.hour, .minute], from: intervalEnd), repeats: false, warningTime: DateComponents(minute: 1)) let newActivity = DeviceActivityName(rawValue: 20minuteUse) Log(😯 (String(describing: schedule.nextInterval))) do { try center.startMonitoring(newActivity, during: schedule) } catch { print(failed to start session: (error.localizedDescription)) } class MyMonitorExtension: DeviceActivityMonitor { let store
2
0
801
Jul ’24
Reply to The DeviceActivityMonitor did not trigger a callback
Below is all the code; the method intervalDidStart has never been called. Can you help me? import UIKit import MobileCoreServices import ManagedSettings import DeviceActivity class MyMonitor: DeviceActivityMonitor { let store = ManagedSettingsStore() override func intervalDidStart(for activity: DeviceActivityName) { super.intervalDidStart(for: activity) print(interval did start) let model = MyModel.shared let applications = model.selectionToDiscourage.applicationTokens store.shield.applications = applications.isEmpty ? nil : applications store.appStore.maximumRating = 200 } override func intervalDidEnd(for activity: DeviceActivityName) { super.intervalDidEnd(for: activity) store.shield.applications = nil store.clearAllSettings() } } // // MyModel.swift // FamilyCotrol // // Created by mac on 2024/6/24. // import UIKit import Foundation import FamilyControls import DeviceActivity import ManagedSettings class MyModel: ObservableObject { static let shared = MyModel() let store = ManagedSettingsStore() p
Topic: App & System Services SubTopic: General Tags:
Jun ’24
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
Jan ’24
Screen Time API is completely UNRELIABLE!
I've been working with the Screen Time API for almost 6 months now. I found out it's completely unreliable, testing on iOS 17.4, the DeviceActivityReport is not showing, the DeviceActivityMonitor more often than not does not fire intervalDidStart. It's very frustrating. Has anyone found out a workaround? We all know there has to be something we're doing wrong, since apps like Opal and Jono does not present those types of issues. Let's please unite our forces and find a solution. How to use this API should not be a secret!
5
0
2.1k
Apr ’24
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
App Shielding unexpectedly active outside of schedule
I'm using the Screen Time API to shield apps the user selects. The problem is, that the apps are always shielded, even outside of the schedule. Here is my function initiateMonitoring: func initiateMonitoring(scheduleStart: DateComponents, scheduleEnd: DateComponents) { let schedule = DeviceActivitySchedule( intervalStart: scheduleStart, intervalEnd: scheduleEnd, repeats: true ) let center = DeviceActivityCenter() do { try center.startMonitoring(.daily, during: schedule) logger.log(STARTED MONITORING) } catch { logger.log(FAILED MONITORING: (error.localizedDescription)) } } The expected result should be that the selected Apps should be blocked during the schedule the user specified, but not outside the schedule, but it is blocked 24/7. I've experimented with the DateComponents, but the issue may be somewhere else. What's maybe interesting is, I tried Logging the DeviceActivityMonitor and somehow it doesn't get called. The logging-output from initiateMonitoring is printed in the console, but for the DeviceActivityMonitor
1
0
829
Mar ’24
Reply to Device Activity Monitor Extension Sometimes Fails To Launch
Same here. Because DeviceActivityMonitor is generally flakey, we emit an event when intervalDidStart isn't called (as determined by intervalDidStart flipping a bit that lives in a UserDefaults container shared with the main app) Here is a graph of those events, broken down by iOS version on which they occurred. It is an iOS 17 problem. Contrast that with all app events, broken down by iOS version and which they occurred. iOS 17.4 is way overrepresented in the first, iOS 16.x is way underrepresented.
Topic: App & System Services SubTopic: General Tags:
Apr ’24
DeviceActivityMonitor Schedule Pause
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 an
1
0
1.4k
Mar ’24