Screen Time API fails to unblock the apps, and leaves an hourglass shield that doesn't allow unshielding

DL;DR: Sometimes the API fails to unblock the app and leaves an hourglass shield on the restricted apps, which is not set by us. Even worse, if we remove authorization of our app from Settings, the restricted apps remain locked until we restart the phone or re-run our app from Xcode.

Our app monitors the usage of selected apps from the current time until the reset time at 4:00 a.m.

When the given threshold is reached, the selected apps are shielded.

After the user taps the close button on the shield view, we attempt to remove the shield, set up a new threshold monitoring and close the app.

The issue is that: sometimes the shield removal fails, and after the app closes itself, it remains shielded, and displays an hourglass shield view that was never in our shield configuration.

Additionally, regardless of what we do-removing the shielding from our apps or removing the authorization of our app from Settings, the restricted apps remain shielded until we restart the phone or re-run our app from Xcode.

Code I use for un-shielding:

warningUsageStore.clearAllSettings()

Code I use for scheduling the threshold:

var warningMinute = userDefaults.integer(forKey: .warningMinute)
if warningMinute <= 0 { warningMinute = 5 }

let selection = familyActivitySelection()

let now = Date.now
let start = Calendar.current.dateComponents([.hour, .minute, .second], from: now)

var end = AppConfig.resetEndTime
// use twenty minutes later as the end time if the before reset time is too near e.g. warning start at 3:55 but reset time is 4:00
if let endDate = Calendar.current.nextDate(after: now, matching: end, matchingPolicy: .nextTime), endDate.timeIntervalSince(now) <= 15*60 {
    end = Calendar.current.dateComponents([.hour, .minute, .second], from: now.addingTimeInterval(20*60))
}


// add inidividual event according to the hash value to dintinguish between apps.
var events = [DeviceActivityEvent.Name : DeviceActivityEvent]()

for application in selection.applications where application.token != nil {
    events[.init(application.hashValue.description)] = .init(applications: [application.token!], threshold: .init(minute: warningMinute))
}
for category in selection.categories where category.token != nil {
    events[.init(category.hashValue.description)] = .init(categories: [category.token!], threshold: .init(minute: warningMinute))
}
for webDomain in selection.webDomains where webDomain.token != nil {
    events[.init(webDomain.hashValue.description)] = .init(webDomains: [webDomain.token!], threshold: .init(minute: warningMinute))
}

try deviceActivityCenter.startMonitoring(.usageWarning, during: .init(intervalStart: start, intervalEnd: end, repeats: false), events: events)

The issue happens on my iPhone11 iOS17.1.2

Any insight would be much appreciated. Thank you!

Post not yet marked as solved Up vote post of startover205 Down vote post of startover205
393 views