Multiple Days Schedules • Screen Time API

Hello,

I wasn't able to figure out how to handle multiple days with DeviceActivitySchedule.

For instance, let's say the user wants to block certain apps from 9:00 AM to 12:00 AM, every day except Saturday and Sunday, how my schedule should look like?

I've tried different things...

This schedule works for each day of the week, but that's not my goal:

let schedule = DeviceActivitySchedule(
            intervalStart: DateComponents(hour: 9, minute: 00),
            intervalEnd: DateComponents(hour: 12, minute: 00),
            repeats: true)

And if I specify the weekDay inside the DateComponents, like this:

// Gregorian calendar
// 2 -> Monday
// 6 -> Friday
let schedule = DeviceActivitySchedule(
            intervalStart: DateComponents(..., weekday: 2),
            intervalEnd: DateComponents(..., weekday: 6),
            repeats: true)

the schedule will block the apps from Monday at 9:00 AM to Friday at 12:00 AM, which is also not my goal.

The only workaround that came to my mind was to create a different schedule for each day of the week:

enum WeekDays: String, CaseIterable {
    case sun, mon, tue, wed, thu, fri, sat
    
    var sortOrder: Int {
        switch self {
        case .sun: return 1
        case .mon: return 2
        case .tue: return 3
        case .wed: return 4
        case .thu: return 5
        case .fri: return 6
        case .sat: return 7
        }
    }
}
func startMonitoring(weekDays: [WeekDays]) {
        
     for weekDay in weekDays {
            
         let day = weekDay.sortOrder
            
         let schedule = DeviceActivitySchedule(
             intervalStart: DateComponents(
                 hour: 9,
                 minute: 00,
                 weekday: day),
             intervalEnd: DateComponents(
                 hour: 12,
                 minute: 00,
                 weekday: day),
                 repeats: true)
            
         let activityName = DeviceActivityName(weekDay.rawValue)
            
         do {
             try center.startMonitoring(activityName, 
                                           during: schedule)
         } catch {
             print("DEBUG: Error: \(error.localizedDescription)")
            }
        }
        
    }

This way I kinda get what I want, for example:

I can specify 3 days of the week, let's say Monday, Tuesday and Wednesday, the time interval, let's keep 9:00 AM - 12:00 AM, and this function will block apps on Monday, Tuesday and Wednesday at that time interval, fine.

However...

What if I also want another schedule that blocks at a different time interval but same day?

For example, I want to block certain apps Monday and Tuesday from 2:00 PM - 6:00 PM.

Following the example above the activityName would be overwritten, so the user ( for Monday and Tuesday ) would now have only the schedules that starts from 2:00 PM.

Basically, I want the user to be able to select multiple days for a schedule and to let them create as many schedules as they want.

Does anybody know the correct way to handle multiple days schedules?

Why don't you just start monitoring that new schedule with a separate activity name? If you use a new activity name, you won't be overwriting the old activity.

Can you show me your code, I'm facing this problem now, I don't know how to set limit apps in daily time period for Monday...Sunday,Thank You Very Much!

Did you find a proper solution? I solved the overwrite problem by setting different activity name but it doesn't look good. Because there is a limit that every app can monitor and if we add in a for loop we can't monitor more than 20 apps?

Hey @Ivan018 were you able to solve this issue? What solution did you go with?

Multiple Days Schedules • Screen Time API
 
 
Q