我試圖在登錄時使用ServiceManagement
自動啓動應用程序。主應用程序(login-Item.app
)在登錄項目列表中註冊啓動器,啓動器啓動主應用程序(2個獨立的目標)。這是我有:Swift:SMLoginItemSetEnabled()在登錄時未啓動
主要應用:
func applicationDidFinishLaunching(_ notification: Notification) {
let helperBundleId = "abc.loginItem-launcher"
let ret = SMLoginItemSetEnabled(helperBundleId as CFString, true)
//'ret' is true at this point
而且從發射:
func applicationDidFinishLaunching(_ aNotification: Notification) {
var alreadyRunning:Bool = false
for app in NSWorkspace.shared().runningApplications{
if app.bundleIdentifier == "abc.login-item"{
alreadyRunning = true
break;
}
}
if !alreadyRunning {
let path = Bundle.main.bundlePath
let index = path.index(path.endIndex, offsetBy:-22)
var newPath = path.substring(to: index)
newPath += "login-item.app"
NSWorkspace.shared().launchApplication(newPath)
}
NSApp.terminate(nil)
}
如果獨立運行,發射成功啓動主應用程序。其他配置(如構建階段)均按教程建議的方式完成,其中包括this之一。我還啓用了沙箱選項,我使用我的開發標識簽署了應用程序,並從「應用程序」文件夾運行應用程序。它按預期工作,但在註銷並重新登錄時不會啓動。
我的應用程序_must_在登錄時運行,用戶不能關閉它,所以我不會實現一個複選框。我不確定是否將此標記爲正確,我的代碼現在可以正常工作(不會更改我發佈的內容),但確實在刪除所有其他存檔時使用了您的建議。之後重新啓動,併成功。謝謝。 – theQuestions
這裏是macOS開發的新手。我花了大量的時間發現「Build」字段的「0.1.1」導致_「Could not resolve ...」_問題。希望我會知道預計會有一個數字值! – Nate