There is no such thing as a error. The results of the debug print assertion I put in is as follows, and it’s working wonderful, so I’m pondering that both it’s not being monitored correctly, or it’s being monitored however eventDidReachThreshold will not be being known as correctly.
Log beneath
・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
Display screen Time Authority Success
Inspired apps choice up to date:
FamilyActivitySelection(includeEntireCategory: true, applicationTokens:
Set([]), categoryTokens: Set([128 bytes: *
<×’Côº6Ä$Ã˫㦧iÎ@`èiìpfS¡úô/Ëss
Ô¬)3´lËN” o¨¨ÓnbHÂ#ì
òKíL+2ñÎVø´¼9~D2mVûVåDäîÉS¬]), webDomainTokens: Set([]),
untokenizedApplicationIdentifiers: Set([]), untokenizedCategoryIdentifiers:
Set([]), untokenizedWebDomainIdentifiers: Set([]))
Organising the schedule
Present time is: 23
Began Monitoring
Supply code beneath
`file: DataModel.swift
//
import Basis
import FamilyControls
import ManagedSettings
non-public let _DataModel = DataModel()
class DataModel: ObservableObject {
let retailer = ManagedSettingsStore()
@Printed var selectionToDiscourage: FamilyActivitySelection
@Printed var selectionToEncourage: FamilyActivitySelection
init() {
let initialApplicationTokens: Set = ["com.example.app1", "com.example.app2"]
selectionToDiscourage = FamilyActivitySelection()
selectionToEncourage = FamilyActivitySelection(includeEntireCategory: true)
}
class var shared: DataModel {
return _DataModel
}
func setShieldRestrictions() {
let purposes = DataModel.shared.selectionToDiscourage
retailer.defend.purposes = purposes.applicationTokens.isEmpty ? nil : purposes.applicationTokens
retailer.defend.applicationCategories = purposes.categoryTokens.isEmpty
? nil
: ShieldSettings.ActivityCategoryPolicy.particular(purposes.categoryTokens)
}
}
file: ScheduleModel.swift
import Basis
import DeviceActivity
import FamilyControls
extension DeviceActivityName {
static let each day = Self("each day")
}
extension DeviceActivityEvent.Identify {
static let inspired = Self("inspired")
}
let schedule = DeviceActivitySchedule(
intervalStart: DateComponents(hour: 0, minute: 0),
intervalEnd: DateComponents(hour: 23, minute: 59),
repeats: true
)
class ScheduleModel :DeviceActivityMonitor {
override func eventDidReachThreshold(_ occasion: DeviceActivityEvent.Identify, exercise: DeviceActivityName) {
tremendous.eventDidReachThreshold(occasion, exercise: exercise)
// イベントが閾値に達したときの処理
print("Occasion (occasion.rawValue) reached threshold for exercise (exercise.rawValue)")
}
// static
public func setSchedule() {
print("Organising the schedule")
print("Present time is: ", Calendar.present.dateComponents([.hour, .minute], from: Date()).hour!)
let heart = DeviceActivityCenter()
let exercise = DeviceActivityName("MyApp.ScreenTime")
let eventName = DeviceActivityEvent.Identify("MyApp.SomeEventName")
let schedule2 = DeviceActivitySchedule(
intervalStart: DateComponents(hour: 0, minute: 0),
intervalEnd: DateComponents(hour: 23, minute: 59),
repeats: true
)
// let heart = DeviceActivityCenter()
do {
// strive heart.startMonitoring(.each day, throughout: schedule, occasions: occasions)
strive heart.startMonitoring(exercise, throughout: schedule2, occasions: [eventName: DeviceActivityEvent(threshold: DateComponents(second: 5))])
print("Began Monitoring")
} catch {
print("Error occured whereas began monitoring: ", error)
}
}
}
file: DeviceActivityExampleApp.swift
import SwiftUI
import FamilyControls
import ManagedSettings
@principal
struct DeviceActivityExampleApp: App {
@UIApplicationDelegateAdaptor non-public var appDelegate: AppDelegate
@StateObject var mannequin = DataModel.shared
@StateObject var retailer = ManagedSettingsStore()
var physique: some Scene {
WindowGroup {
ContentView()
.environmentObject(mannequin)
.environmentObject(retailer)
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
func software(_ software: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// 非同期タスクで authorize() メソッドを呼び出します
Activity {
do {
strive await authorize()
print("スクリーンタイム権限成功")
} catch {
print("Authorization failed with error: (error)")
}
}
// ScheduleModel.setSchedule()
// let monitor = MyMonitor()
// monitor.startMonitoring()
return true
}
func authorize() async throws {
strive await AuthorizationCenter.shared.requestAuthorization(for: .particular person)
}
}
file: ContentView.swift
//
// ContentView.swift
// DeviceActivityExample
//
//
import SwiftUI
struct ContentView: View {
@State non-public var isDiscouragedPresented = false
@State non-public var isEncouragedPresented = false
@EnvironmentObject var mannequin: DataModel
var physique: some View {
VStack {
Button("Choose Apps to Discourage") {
isDiscouragedPresented = true
}
.familyActivityPicker(isPresented: $isDiscouragedPresented, choice: $mannequin.selectionToDiscourage)
.onChange(of: mannequin.selectionToDiscourage) { newSelection in
DataModel.shared.setShieldRestrictions()
print("Discouraged apps choice up to date: (newSelection)")
}
Button("Choose Apps to Encourage") {
isEncouragedPresented = true
}
.familyActivityPicker(isPresented: $isEncouragedPresented, choice: $mannequin.selectionToEncourage)
.onChange(of: mannequin.selectionToEncourage) { newSelection in
// DataModel.shared.setShieldRestrictions()
print("Inspired apps choice up to date: (newSelection)")
let monitor = ScheduleModel()
ScheduleModel().setSchedule()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environmentObject(DataModel())
}
}
file: information.plist
NSExtension
NSExtensionPointIdentifier
com.apple.deviceactivity.monitor-extension
NSExtensionPrincipalClass
$(PRODUCT_MODULE_NAME).DeviceActivityMonitorExtension
I searched teratail, Google, and so on.
I modified the supply code in my very own means.
I needed to name the method when the edge was reached from DeviceActivityMonitorExtension.swift, nevertheless it didn't work, so I believed to name it as soon as in ScheduleModel.swift, however the end result was the identical.`