I’ve never really done anything with Content Blockers, but…
If in UIKit you would call
SFContentBlockerManager.reloadContentBlocker(withIdentifier:completionHandler:)
in the app delegate’s applicationDidBecomeActive
method, then when using SwiftUI, you should be able to call it by doing something like this:
struct MyContentBlockingApp: App {
@Environment(\.scenePhase) var scenePhase
//whatever else you gotta do here
var body: some Scene {
WindowGroup {
ContentView()
.onChange(of: scenePhase) { newPhase in
switch newPhase {
case .active:
SFContentBlockerManager.reloadContentBlocker(withIdentifier: contentBlockerId, completionHandler: nil)
case .inactive:
//do whatever when your app becomes inactive
case .background:
//do whatever when your app goes into the background
@unknown default:
print("unknown phase")
}
}
}
}
}
Or you could watch out for a notification, like so:
struct MyContentBlockingApp: App {
//blah blah blah
var body: some Scene {
WindowGroup {
ContentView()
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
SFContentBlockerManager.reloadContentBlocker(withIdentifier: contentBlockerId, completionHandler: nil)
}
}
}
}
You can also add an app delegate to a SwiftUI app: How to add an AppDelegate to a SwiftUI app
But, like I said, I’ve never really done anything with content blocking, so this may not work for your purposes. Worth a try, though.