Add button to uninstall system extension
This commit is contained in:
@@ -101,8 +101,19 @@ public struct SettingView: View {
|
|||||||
Task {
|
Task {
|
||||||
do {
|
do {
|
||||||
if let result = try await SystemExtension.install(forceUpdate: true) {
|
if let result = try await SystemExtension.install(forceUpdate: true) {
|
||||||
if result == .willCompleteAfterReboot {
|
switch result {
|
||||||
alert = Alert(errorMessage: "Need reboot")
|
case .completed:
|
||||||
|
alert = Alert(
|
||||||
|
title: Text("Update"),
|
||||||
|
message: Text("System Extension updated."),
|
||||||
|
dismissButton: .default(Text("Ok")) {}
|
||||||
|
)
|
||||||
|
case .willCompleteAfterReboot:
|
||||||
|
alert = Alert(
|
||||||
|
title: Text("Update"),
|
||||||
|
message: Text("Reboot required."),
|
||||||
|
dismissButton: .default(Text("Ok")) {}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
@@ -110,6 +121,32 @@ public struct SettingView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Button {
|
||||||
|
Task {
|
||||||
|
do {
|
||||||
|
if let result = try await SystemExtension.uninstall() {
|
||||||
|
switch result {
|
||||||
|
case .completed:
|
||||||
|
alert = Alert(
|
||||||
|
title: Text("Uninstall"),
|
||||||
|
message: Text("System Extension removed."),
|
||||||
|
dismissButton: .default(Text("Ok")) {}
|
||||||
|
)
|
||||||
|
case .willCompleteAfterReboot:
|
||||||
|
alert = Alert(
|
||||||
|
title: Text("Uninstall"),
|
||||||
|
message: Text("Reboot required."),
|
||||||
|
dismissButton: .default(Text("Ok")) {}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
alert = Alert(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} label: {
|
||||||
|
Text("Uninstall System Extension").foregroundColor(.red)
|
||||||
|
}
|
||||||
}.frame(maxWidth: .infinity, alignment: .trailing)
|
}.frame(maxWidth: .infinity, alignment: .trailing)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
private var properties: [OSSystemExtensionProperties]?
|
private var properties: [OSSystemExtensionProperties]?
|
||||||
private var error: Error?
|
private var error: Error?
|
||||||
|
|
||||||
private init(forceUpdate: Bool = false, inBackground: Bool = false) {
|
private init(_ forceUpdate: Bool = false, _ inBackground: Bool = false) {
|
||||||
self.forceUpdate = forceUpdate
|
self.forceUpdate = forceUpdate
|
||||||
self.inBackground = inBackground
|
self.inBackground = inBackground
|
||||||
}
|
}
|
||||||
@@ -53,7 +53,7 @@
|
|||||||
semaphore.signal()
|
semaphore.signal()
|
||||||
}
|
}
|
||||||
|
|
||||||
public func submitAndWait() throws -> OSSystemExtensionRequest.Result? {
|
public func activation() throws -> OSSystemExtensionRequest.Result? {
|
||||||
let request = OSSystemExtensionRequest.activationRequest(forExtensionWithIdentifier: FilePath.packageName + ".system", queue: .main)
|
let request = OSSystemExtensionRequest.activationRequest(forExtensionWithIdentifier: FilePath.packageName + ".system", queue: .main)
|
||||||
request.delegate = self
|
request.delegate = self
|
||||||
OSSystemExtensionManager.shared.submitRequest(request)
|
OSSystemExtensionManager.shared.submitRequest(request)
|
||||||
@@ -64,6 +64,17 @@
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func deactivation() throws -> OSSystemExtensionRequest.Result? {
|
||||||
|
let request = OSSystemExtensionRequest.deactivationRequest(forExtensionWithIdentifier: FilePath.packageName + ".system", queue: .main)
|
||||||
|
request.delegate = self
|
||||||
|
OSSystemExtensionManager.shared.submitRequest(request)
|
||||||
|
semaphore.wait()
|
||||||
|
if let error {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
public func getProperties() throws -> [OSSystemExtensionProperties] {
|
public func getProperties() throws -> [OSSystemExtensionProperties] {
|
||||||
let request = OSSystemExtensionRequest.propertiesRequest(forExtensionWithIdentifier: FilePath.packageName + ".system", queue: .main)
|
let request = OSSystemExtensionRequest.propertiesRequest(forExtensionWithIdentifier: FilePath.packageName + ".system", queue: .main)
|
||||||
request.delegate = self
|
request.delegate = self
|
||||||
@@ -100,9 +111,15 @@
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func install(forceUpdate: Bool = false, inBackground _: Bool = false) async throws -> OSSystemExtensionRequest.Result? {
|
public static func install(forceUpdate: Bool = false, inBackground: Bool = false) async throws -> OSSystemExtensionRequest.Result? {
|
||||||
try await Task.detached {
|
try await Task.detached {
|
||||||
try SystemExtension(forceUpdate: forceUpdate).submitAndWait()
|
try SystemExtension(forceUpdate, inBackground).activation()
|
||||||
|
}.result.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func uninstall() async throws -> OSSystemExtensionRequest.Result? {
|
||||||
|
try await Task.detached {
|
||||||
|
try SystemExtension().deactivation()
|
||||||
}.result.get()
|
}.result.get()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user