OK. So in a SwiftUI app the type that conforms to App isn’t a class, it’s a struct. But the OSSystemExtensionRequest delegate must be an object. That suggests you’ve created some sort of manager object for this process.
I’ve seen problems like this where the app wasn’t doing anything to hold the delegate in memory, and thus it got released while the request was still in flight (OSSystemExtensionRequest itself only maintains a weak reference). Are you sure that’s not happening here?
I'm using a static variable in the App class trying to prevent that from happening:
@main
struct dns_proxy_testsApp: App {
static let systemExtensionManager = SystemExtensionManager()
var body: some Scene {
WindowGroup {
ContentView()
}
}
init() {
Self.systemExtensionManager.activate()
configureDNSProxy()
}
}
I also added the deinit to investigate, and it's never called.
This is the full class implementation of the delegate
class SystemExtensionManager: NSObject, OSSystemExtensionRequestDelegate {
func activate() {
let request = OSSystemExtensionRequest.activationRequest(forExtensionWithIdentifier: "com.myteam.dns-proxy-tests.ne",
queue: DispatchQueue.main)
request.delegate = self
let extensionManager = OSSystemExtensionManager.shared
print("Submitting request to activate system extension...")
extensionManager.submitRequest(request)
}
func request(_ request: OSSystemExtensionRequest, actionForReplacingExtension existing: OSSystemExtensionProperties, withExtension ext: OSSystemExtensionProperties) -> OSSystemExtensionRequest.ReplacementAction {
print("Replacing extension: \(existing.bundleIdentifier) with \(ext.bundleIdentifier)")
return .replace
}
func requestNeedsUserApproval(_ request: OSSystemExtensionRequest) {
print("Request \(request) needs user approval.")
}
func request(_ request: OSSystemExtensionRequest, didFinishWithResult result: OSSystemExtensionRequest.Result) {
print("Request finished with result: \(result)")
}
func request(_ request: OSSystemExtensionRequest, didFailWithError error: any Error) {
print("Request failed with error: \(error.localizedDescription)")
}
deinit {
print("Deinitializing SystemExtensionManager")
}
}
I also tried defining it as a global variable instead of static but I don't see any change.