macOS Ventura下Mac Studio机型可读名称获取失败的解决方案咨询
问题分析与解决方法
在macOS Ventura及后续版本中,部分新设备(如Mac Studio Mac13,1)的营销名称不再存储于ServerInformation.framework的SIMachineAttributes.plist中,因此原代码无法获取对应信息。以下是几种可行的替代实现方式:
方案一:使用IOKit API直接获取设备名称
该方法通过IOKit的电源服务接口获取设备官方营销名称,无需读取plist文件,兼容性更强:
import IOKit.power public static var marketingModel: String { let powerSourceService = IOServiceGetMatchingService(kIOMainPortDefault, IOServiceMatching("IOPMPowerSource")) defer { IOObjectRelease(powerSourceService) } guard let productName = IOPMCopyProductName(powerSourceService)?.takeRetainedValue() as? String else { return String.hyphen } return productName }
注:此方法返回的名称如"Mac Studio",部分设备可能不包含尺寸和年份信息(如MacBook Pro需额外处理),但胜在稳定可靠。
方案二:读取PlatformSupport.framework中的设备信息
Ventura将新设备的属性信息迁移到了PlatformSupport.framework的plist文件中,路径为/System/Library/PrivateFrameworks/PlatformSupport.framework/PlatformSupport.plist。该plist包含ModelProperties字段,以modelIdentifier为key,对应的value中包含marketingName字段。实现代码如下:
public static var marketingModel: String { let modelIdentifier = self.modelIdentifier let plistPath = "/System/Library/PrivateFrameworks/PlatformSupport.framework/PlatformSupport.plist" guard let fileData = FileManager.default.contents(atPath: plistPath), let plistContents = try? PropertyListSerialization.propertyList(from: fileData, format: nil) as? [String: Any], let modelProperties = plistContents["ModelProperties"] as? [String: [String: Any]], let deviceInfo = modelProperties[modelIdentifier], let marketingName = deviceInfo["marketingName"] as? String else { return String.hyphen } return marketingName }
注:可将此路径与原SIMachineAttributes.plist的读取逻辑结合,优先尝试新路径,失败后回退到原路径,覆盖新老设备的兼容需求。
方案三:结合系统工具命令行获取(备选)
通过执行system_profiler命令行工具提取硬件信息,适合快速验证但不推荐在正式App中使用(存在性能和权限风险):
public static var marketingModel: String { let task = Process() task.executableURL = URL(fileURLWithPath: "/usr/sbin/system_profiler") task.arguments = ["SPHardwareDataType", "-json"] let pipe = Pipe() task.standardOutput = pipe do { try task.run() let data = pipe.fileHandleForReading.readDataToEndOfFile() guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any], let hardwareInfo = json["SPHardwareDataType"] as? [[String: Any]], let modelName = hardwareInfo.first?["machine_name"] as? String else { return String.hyphen } return modelName } catch { return String.hyphen } }
内容的提问来源于stack exchange,提问作者inexcitus
相关产品推荐
相关产品推荐

