I am working on a SwiftUI app using the Screen Time API and the DeviceActivityReport view to display app usage data. My current implementation successfully shows daily app usage using a DeviceActivityFilter with the .daily(during:) segment. However, I need to filter this data to show app usage only for a specific time period during the day, e.g., 4:00 PM to 5:00 PM.
I created a DeviceActivityFilter with a .daily(during:) segment and passed a DateInterval for the desired time range:
let now = Date()
let startTime = calendar.date(bySettingHour: 16, minute: 0, second: 0, of: now)!
let endTime = calendar.date(bySettingHour: 17, minute: 0, second: 0, of: now)!
let timeInterval = DateInterval(start: startTime, end: endTime)
let filter = DeviceActivityFilter(
segment: .daily(during: timeInterval),
users: .all,
devices: .init([.iPhone])
)
I applied this filter to the DeviceActivityReport view:
DeviceActivityReport(context, filter: filter)
Even with the DateInterval set for the specific time range, the report still shows the total daily usage for each app, instead of restricting the results to the specified 1:00 PM to 5:00 PM range.