You can do something like this:
Unlock app for certain period of time via ShieldActionExtension, during this unlock process save that app using it's Token in shared preference (Shared User Defaults).
You can save using this procedure:
struct ApplicationProfile: Codable, Hashable {
let id: UUID
let applicationToken: ApplicationToken
init(id: UUID = UUID(), applicationToken: ApplicationToken) {
self.applicationToken = applicationToken
self.id = id
}
}
// You can get this token from ShieldAction delegate method handle action
let profile = ApplicationProfile(applicationToken: token)
SharedData.addApplicationProfile(profile)
The app you have unlocked is saved, now schedule an event according to your desired threshold using the profile you have in your shared cache.
let unlockTime = 900 // Time in seconds
let threshold = DateComponents(second: unlockTime)
let event: [DeviceActivityEvent.Name: DeviceActivityEvent] = [
DeviceActivityEvent.Name(profile.id.uuidString): DeviceActivityEvent(
applications: Set<ApplicationToken>([application]),
threshold: threshold
)
]
let center = DeviceActivityCenter()
let schedule = DeviceActivitySchedule(
intervalStart: DateComponents(hour: 0, minute: 0),
intervalEnd: DateComponents(hour: 23, minute: 59),
repeats: true
)
do {
try center.startMonitoring(DeviceActivityName(profile.id.uuidString), during: schedule, events: event)
} catch {
print("Device monitoring error: \(error)")
}
}
This will schedule a device activity for 24 hours, which will monitor the app usage.
Once that threshold reached (user have used app for 15 minutes in your case) your DeviceActivityMonitorExtension delegate method eventDidReachThreshold will get called
Now lock the app again.
override func eventDidReachThreshold(_ event: DeviceActivityEvent.Name, activity: DeviceActivityName) {
super.eventDidReachThreshold(event, activity: activity)
// Handle the event reaching its threshold.
guard let activityId = UUID(uuidString: activity.rawValue) else { return }
guard let application = SharedData.getApplicationProfile(id: activityId) else { return }
store.shield.applications?.insert(application.applicationToken)
SharedData.removeApplicationProfile(application)
logger.log("DeviceActivityMonitorExtension: Apps shield set again")
}
In this way you will be able to apply shield on respective app after your threshold reached.
Hope that helps you.
Topic:
App & System Services
SubTopic:
General
Tags: