I added debug break point to the first line of MyDeviceActivityMonitor.swift, it never got trigger. I am able to launch the app, select discouraged and encouraged app. discouraged apps are getting restricted which is expected. but device activity extension is never called.
I have all the permission and certificate. I created an app group.
import SwiftUI
import FamilyControls
import ManagedSettings
@main
struct GetALifeApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
@StateObject var model = MyModel.shared
@StateObject var store = ManagedSettingsStore()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(model)
.environmentObject(store)
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
initiateAsyncSetup()
MySchedule.setSchedule()
return true
}
private func initiateAsyncSetup() {
Task {
do {
try await AuthorizationCenter.shared.requestAuthorization(for: .individual)
} catch {
print("Error during asynchronous setup: \(error)")
}
}
}
}
import Foundation
import FamilyControls
import ManagedSettings
private let _MyModel = MyModel()
class MyModel: ObservableObject {
let store = ManagedSettingsStore()
@Published var selectionToDiscourage: FamilyActivitySelection
@Published var selectionToEncourage: FamilyActivitySelection
init() {
selectionToDiscourage = FamilyActivitySelection()
selectionToEncourage = FamilyActivitySelection()
}
class var shared: MyModel {
return _MyModel
}
func setShieldRestrictions() {
let applications = MyModel.shared.selectionToDiscourage
store.shield.applications = applications.applicationTokens.isEmpty ? nil : applications.applicationTokens
store.shield.applicationCategories = applications.categoryTokens.isEmpty
? nil
: ShieldSettings.ActivityCategoryPolicy.specific(applications.categoryTokens)
}
}
import Foundation
import DeviceActivity
extension DeviceActivityName {
static let daily = Self("daily")
}
extension DeviceActivityEvent.Name {
static let encouraged = Self("encouraged")
}
let schedule = DeviceActivitySchedule(
intervalStart: DateComponents(hour: 0, minute: 0),
intervalEnd: DateComponents(hour: 23, minute: 59),
repeats: true
)
class MySchedule {
static public func setSchedule() {
print("Setting schedule...")
print("Hour is: ", Calendar.current.dateComponents([.hour, .minute], from: Date()).hour!)
let events: [DeviceActivityEvent.Name: DeviceActivityEvent] = [
.encouraged: DeviceActivityEvent(
applications: MyModel.shared.selectionToEncourage.applicationTokens,
threshold: DateComponents(second: 2)
)
]
let center = DeviceActivityCenter()
do {
print("Try to start monitoring...")
print(events)
try center.startMonitoring(.daily, during: schedule, events: events)
} catch {
print("Error monitoring schedule: ", error)
}
}
}
import SwiftUI
struct ContentView: View {
@State private var isDiscouragedPresented = false
@State private var isEncouragedPresented = false
@EnvironmentObject var model: MyModel
var body: some View {
VStack {
Button("Select Apps to Discourage") {
isDiscouragedPresented = true
}
.familyActivityPicker(isPresented: $isDiscouragedPresented, selection: $model.selectionToDiscourage)
Button("Select Apps to Encourage") {
isEncouragedPresented = true
}
.familyActivityPicker(isPresented: $isEncouragedPresented, selection: $model.selectionToEncourage)
}
.onChange(of: model.selectionToDiscourage) {
MyModel.shared.setShieldRestrictions()
}
.onChange(of: model.selectionToEncourage) {
MySchedule.setSchedule()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environmentObject(MyModel())
}
}
import Foundation
import DeviceActivity
import ManagedSettings
class MyDeviceActivityMonitor: DeviceActivityMonitor {
let store = ManagedSettingsStore()
override func intervalDidStart(for activity: DeviceActivityName) {
print("intervalDidStart")
super.intervalDidStart(for: activity)
store.shield.applications = nil
print("intervalDidStart")
}
override func intervalDidEnd(for activity: DeviceActivityName) {
super.intervalDidEnd(for: activity)
}
override func eventDidReachThreshold(_ event: DeviceActivityEvent.Name, activity: DeviceActivityName) {
super.eventDidReachThreshold(event, activity: activity)
print("used encouraged")
store.shield.applications = nil
}
override func intervalWillStartWarning(for activity: DeviceActivityName) {
super.intervalWillStartWarning(for: activity)
// Handle the warning before the interval starts.
}
override func intervalWillEndWarning(for activity: DeviceActivityName) {
super.intervalWillEndWarning(for: activity)
// Handle the warning before the interval ends.
}
override func eventWillReachThresholdWarning(_ event: DeviceActivityEvent.Name, activity: DeviceActivityName) {
super.eventWillReachThresholdWarning(event, activity: activity)
// Handle the warning before the event reaches its threshold.
}
}