Skip to content

Commit f4c944e

Browse files
committed
Release 1.1.0
1 parent c827c48 commit f4c944e

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

Sources/SystemExtensionKit/SystemExtensionKit.swift

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import SystemExtensions
1212
#error("SystemExtensionKit doesn't support Swift versions below 5.5.")
1313
#endif
1414

15-
/// Current SystemExtensionKit version. Necessary since SPM doesn't use dynamic libraries. Plus this will be more accurate.
16-
let version = "1.0.0"
15+
/// Current SystemExtensionKit version 1.1.0. Necessary since SPM doesn't use dynamic libraries. Plus this will be more accurate.
16+
public let version = "1.1.0"
1717

1818
public let SystemExtension = SystemExtensionKit.shared
1919

@@ -54,9 +54,10 @@ public class SystemExtensionKit: NSObject {
5454
case failed(OSSystemExtensionRequest, Error)
5555
case needsUserApproval(OSSystemExtensionRequest)
5656
case replacingExtension(OSSystemExtensionRequest, String, String)
57+
case cancelExtension(OSSystemExtensionRequest, String, String)
5758
}
5859

59-
static let shared = SystemExtensionKit()
60+
public static let shared = SystemExtensionKit()
6061
override private init() {}
6162

6263
public weak var delegate: SystemExtensionDelegate?
@@ -74,7 +75,10 @@ public class SystemExtensionKit: NSObject {
7475
return bundle
7576
}
7677

77-
public func activeSystemExtension() async throws {
78+
private var needForceUpdate: Bool = false
79+
80+
public func activeSystemExtension(forceUpdate: Bool = false) async throws {
81+
needForceUpdate = forceUpdate
7882
// 请求 SystemExtension 授权
7983
try await withCheckedThrowingContinuation { (cont: CheckedContinuation<Void, Error>) in
8084
do {
@@ -96,8 +100,8 @@ public class SystemExtensionKit: NSObject {
96100

97101
@available(macOS 12.0, *)
98102
public func checkSystemExtensionEnableStatus() async -> Bool {
99-
if let _ = try? await enabledSystemExtensionProperty() {
100-
return true
103+
if let property = try? await enabledSystemExtensionProperty() {
104+
return !property.isAwaitingUserApproval && !property.isUninstalling
101105
} else {
102106
return false
103107
}
@@ -152,10 +156,34 @@ extension SystemExtensionKit: OSSystemExtensionRequestDelegate {
152156
}
153157

154158
public func request(_ request: OSSystemExtensionRequest, actionForReplacingExtension existing: OSSystemExtensionProperties, withExtension extension: OSSystemExtensionProperties) -> OSSystemExtensionRequest.ReplacementAction {
155-
let existingVersion = existing.bundleShortVersion
156-
let extensionVersion = `extension`.bundleShortVersion
157-
delegate?.systemExtensionKit(self, requestResult: .replacingExtension(request, existingVersion, extensionVersion))
158-
return .replace
159+
if needForceUpdate {
160+
return .replace
161+
}
162+
if #available(macOS 12.0, *) {
163+
if existing.isAwaitingUserApproval {
164+
return .replace
165+
}
166+
}
167+
// existing
168+
let existingBundleIdentifier = existing.bundleIdentifier
169+
let existingBundleVersion = existing.bundleVersion
170+
let existingBundleShortVersion = existing.bundleShortVersion
171+
172+
// `extension`
173+
let extensionBundleIdentifier = `extension`.bundleIdentifier
174+
let extensionBundleVersion = `extension`.bundleVersion
175+
let extensionBundleShortVersion = `extension`.bundleShortVersion
176+
177+
guard existingBundleIdentifier == extensionBundleIdentifier,
178+
existingBundleVersion == extensionBundleVersion,
179+
existingBundleShortVersion == extensionBundleShortVersion
180+
else {
181+
delegate?.systemExtensionKit(self, requestResult: .replacingExtension(request, existingBundleShortVersion, extensionBundleShortVersion))
182+
return .replace
183+
}
184+
185+
delegate?.systemExtensionKit(self, requestResult: .replacingExtension(request, existingBundleShortVersion, extensionBundleShortVersion))
186+
return .cancel
159187
}
160188

161189
@available(macOS 12.0, *)

0 commit comments

Comments
 (0)