|
| 1 | +import Foundation |
| 2 | +import AppKit |
| 3 | + |
| 4 | +// MARK: - LaunchServices Private API |
| 5 | + |
| 6 | +// LSApplicationProxy 私有类(用于获取应用信息) |
| 7 | +@objc protocol LSApplicationProxy { |
| 8 | + var bundleIdentifier: String { get } |
| 9 | + var bundleURL: URL { get } |
| 10 | +} |
| 11 | + |
| 12 | +// LSApplicationWorkspace 私有类(用于卸载应用) |
| 13 | +@objc protocol LSApplicationWorkspace { |
| 14 | + static func defaultWorkspace() -> Self |
| 15 | + func uninstallApplication(_ bundleIdentifier: String, withOptions options: [String: Any]?) -> Bool |
| 16 | + func openApplicationWithBundleID(_ bundleIdentifier: String) -> Bool |
| 17 | +} |
| 18 | + |
| 19 | +// MARK: - App Uninstall Service |
| 20 | + |
| 21 | +/// 应用卸载服务,使用 LaunchServices 私有 API 实现真正的卸载功能 |
| 22 | +final class AppUninstallService { |
| 23 | + |
| 24 | + static let shared = AppUninstallService() |
| 25 | + |
| 26 | + private init() {} |
| 27 | + |
| 28 | + // MARK: - Public Methods |
| 29 | + |
| 30 | + /// 卸载应用 |
| 31 | + /// - Parameters: |
| 32 | + /// - bundleIdentifier: 应用的 Bundle ID |
| 33 | + /// - completion: 完成回调,返回是否成功 |
| 34 | + func uninstallApp(bundleIdentifier: String, completion: @escaping (Bool, Error?) -> Void) { |
| 35 | + // 1. 先尝试使用私有 API 卸载 |
| 36 | + if uninstallUsingPrivateAPI(bundleIdentifier: bundleIdentifier) { |
| 37 | + completion(true, nil) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + // 2. 如果私有 API 失败,回退到移到废纸篓 |
| 42 | + fallbackToTrash(bundleIdentifier: bundleIdentifier, completion: completion) |
| 43 | + } |
| 44 | + |
| 45 | + // MARK: - Private Methods |
| 46 | + |
| 47 | + /// 使用 LaunchServices 私有 API 卸载 |
| 48 | + private func uninstallUsingPrivateAPI(bundleIdentifier: String) -> Bool { |
| 49 | + // 获取 LSApplicationWorkspace 类 |
| 50 | + guard let workspaceClass = NSClassFromString("LSApplicationWorkspace") as? NSObject.Type else { |
| 51 | + return false |
| 52 | + } |
| 53 | + |
| 54 | + // 获取 defaultWorkspace |
| 55 | + guard let workspace = workspaceClass.perform(NSSelectorFromString("defaultWorkspace"))?.takeUnretainedValue() else { |
| 56 | + return false |
| 57 | + } |
| 58 | + |
| 59 | + // 调用 uninstallApplication:withOptions: |
| 60 | + let selector = NSSelectorFromString("uninstallApplication:withOptions:") |
| 61 | + guard workspace.responds(to: selector) else { |
| 62 | + return false |
| 63 | + } |
| 64 | + |
| 65 | + // 创建方法签名 |
| 66 | + guard let method = class_getInstanceMethod(type(of: workspace), selector) else { |
| 67 | + return false |
| 68 | + } |
| 69 | + |
| 70 | + let implementation = method_getImplementation(method) |
| 71 | + typealias UninstallFunction = @convention(c) (AnyObject, Selector, NSString, NSDictionary?) -> Bool |
| 72 | + let uninstallFunc = unsafeBitCast(implementation, to: UninstallFunction.self) |
| 73 | + |
| 74 | + // 执行卸载 |
| 75 | + let options: NSDictionary? = nil |
| 76 | + let result = uninstallFunc(workspace as AnyObject, selector, bundleIdentifier as NSString, options) |
| 77 | + |
| 78 | + return result |
| 79 | + } |
| 80 | + |
| 81 | + /// 回退方案:移到废纸篓 |
| 82 | + private func fallbackToTrash(bundleIdentifier: String, completion: @escaping (Bool, Error?) -> Void) { |
| 83 | + guard let appURL = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleIdentifier) else { |
| 84 | + completion(false, NSError(domain: "AppUninstallService", code: -1, userInfo: [ |
| 85 | + NSLocalizedDescriptionKey: "Application not found" |
| 86 | + ])) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + NSWorkspace.shared.recycle([appURL]) { trashedURLs, error in |
| 91 | + if let error = error { |
| 92 | + completion(false, error) |
| 93 | + } else { |
| 94 | + completion(!trashedURLs.isEmpty, nil) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments