背景
更新完系統(tǒng)局义,發(fā)現(xiàn)自己一個mac應(yīng)用開機(jī)自啟失敗。
表現(xiàn)為方法調(diào)用返回 false
SMLoginItemSetEnabled(loginItemIdentifier as CFString, state)
查詢發(fā)現(xiàn)macOS 13.0 更新了一個新類 SMAppService
,使用這個類調(diào)用register()
and unregister()
image.png
解決辦法
// 創(chuàng)建登錄項
@available(macOS 13.0, *)
private func loginItem() -> SMAppService {
// The bundle identifier of the helper application bundle
return SMAppService.loginItem(identifier: loginItemIdentifier)
}
// 注冊登錄項
@available(macOS 13.0, *)
func addLoginItem(complate: (_ result: Bool) -> ()) {
do {
try loginItem().register()
saveLoginAutoLaunchState(state: true)
complate(true)
} catch {
print(error)
complate(false)
}
}
// 移除登錄項
@available(macOS 13.0, *)
func removeLoginItem(complate: (_ result: Bool) -> ()) {
do {
try loginItem().unregister()
saveLoginAutoLaunchState(state: false)
complate(true)
} catch {
print(error)
complate(false)
}
}
Xcode 需要更新到14.1忿危。正式暫時沒更新,可以用RC版
官方文檔
https://developer.apple.com/documentation/servicemanagement/smappservice