I'm having trouble with my DeviceActivityMonitorExtension. The intervalDidStart function is not being called when the scheduler is created. Does anyone have an idea why this is?
let schedule = DeviceActivitySchedule(
intervalStart: DateComponents(hour: 15, minute: 23),
intervalEnd: DateComponents(hour: 16, minute: 55),
repeats: true
)
class MySchedule {
static public func setSchedule() {
let center = DeviceActivityCenter()
center.stopMonitoring([.daily])
do {
try center.startMonitoring(.daily, during: schedule)
} catch {
print("Error monitoring schedule: ", error)
}
}
}
class DeviceActivityMonitorExtension: DeviceActivityMonitor {
override func intervalDidStart(for activity: DeviceActivityName) {
super.intervalDidStart(for: activity)
SelectedApps.shared.setRestrictions()
}
private let _SelectedApps = SelectedApps()
class SelectedApps: ObservableObject{
@Published var selection: FamilyActivitySelection
let store = ManagedSettingsStore()
init() {
if let savedSelection = UserDefaults.standard.object(forKey: "savedSelection") as? Data {
let decoder = JSONDecoder()
if let loadedSelection = try? decoder.decode(FamilyActivitySelection.self, from: savedSelection) {
selection = loadedSelection
} else {
selection = FamilyActivitySelection(includeEntireCategory: true)
}
} else {
selection = FamilyActivitySelection(includeEntireCategory: true)
}
}
class var shared: SelectedApps {
return _SelectedApps
}
func setRestrictions(){
let applications = selection
store.shield.applications = applications.applicationTokens.isEmpty ? nil : applications.applicationTokens
store.shield.applicationCategories = applications.categoryTokens.isEmpty
? nil
: ShieldSettings.ActivityCategoryPolicy.specific(applications.categoryTokens)
}
Post
Replies
Boosts
Views
Activity
I'm trying to modify a ShieldConfigurationExtension, but am having trouble adding an icon. I have tried directly adding a png to the ShieldConfiguration extension folder and creating a UIImage from it, but it doesn't seem to work. Am I working with images within an extension properly?
This code displays the default configuration
override func configuration(shielding application: Application, in category: ActivityCategory) -> ShieldConfiguration {
// Customize the shield as needed for applications shielded because of their category.
let image = UIImage(named: "icon")
return ShieldConfiguration( icon: image)
}
This code properly displays a customized configuration
return ShieldConfiguration( title: ShieldConfiguration.Label(text: "test", color: .white))
I am trying to filter my DeviceActivityReport to only show activity for the specific app tokens I pass in. Right now, it shows activity for all apps. Is there something else I need to do in makeConfiguration so that it only filters the application tokens that I'm filtering by?
filter = DeviceActivityFilter(segment: .hourly(during: dateInterval), applications: task.selection.applicationTokens)
struct TotalActivityReport: DeviceActivityReportScene {
// Define which context your scene will represent.
let context: DeviceActivityReport.Context = .totalActivity
// Define the custom configuration and the resulting view for this report.
let content: (String) -> TotalActivityView
func makeConfiguration(representing data: DeviceActivityResults<DeviceActivityData>) async -> String {
// Reformat the data into a configuration that can be used to create
// the report's view.
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.minute, .second]
formatter.unitsStyle = .abbreviated
formatter.zeroFormattingBehavior = .dropAll
let totalActivityDuration = await data.flatMap { $0.activitySegments }.reduce(0, {
$0 + $1.totalActivityDuration
})
return formatter.string(from: totalActivityDuration) ?? "No activity data"
}
}