实现了帮助应用程序,但在登录时未启动

saf*_*yeh 7 macos swift

我需要在用户登录时启动我的Mac应用程序.

这就是我做的:

我创建了一个新的Coca Application Target.

•构建设置将"跳过安装"设置为"是"

•Info.plist"应用程序仅后台"到"是"

•在主应用程序和帮助程序应用程序上启用了沙盒.

•将复制文件阶段添加到主应用程序:Wrapper,Contents/Library/LoginItems,添加了Helper.app

Helper应用程序的AppDelegate.swift

import Cocoa
import ServiceManagement
extension Notification.Name {
    static let killLauncher = Notification.Name("killLauncher")
}

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @objc func terminate() {
        NSApp.terminate(nil)
    }

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        print("hi")
        let mainAppIdentifier = "co.myprogress.osx"
        let runningApps = NSWorkspace.shared.runningApplications
        let isRunning = !runningApps.filter { $0.bundleIdentifier == mainAppIdentifier }.isEmpty

        if !isRunning {
            DistributedNotificationCenter.default().addObserver(self,
                                                                selector: #selector(self.terminate),
                                                                name: .killLauncher,
                                                                object: mainAppIdentifier)

            let path = Bundle.main.bundlePath as NSString
            var components = path.pathComponents
            components.removeLast()
            components.removeLast()
            components.removeLast()
            components.append("MacOS")
            components.append("TODOs Menubar") //main app name

            let newPath = NSString.path(withComponents: components)

            NSWorkspace.shared.launchApplication(newPath)
        }
        else {
            self.terminate()
        }
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }


}
Run Code Online (Sandbox Code Playgroud)

主应用程序的AppDelegate.swift

func applicationDidFinishLaunching(_ aNotification: Notification) {
        let launcherAppId = "co.myprogress.TodosMenubarHelper"
        let runningApps = NSWorkspace.shared().runningApplications
        let isRunning = !runningApps.filter { $0.bundleIdentifier == launcherAppId }.isEmpty

        let ret = SMLoginItemSetEnabled(launcherAppId as CFString, true)
        print(ret)

        if isRunning {
            DistributedNotificationCenter.default().post(name: .killLauncher,
                                                         object: Bundle.main.bundleIdentifier!)
        }

    }
Run Code Online (Sandbox Code Playgroud)

测试

•Cmd + B来构建应用程序

•右键单击产品>在Finder中显示.app

•推出应用程序

• 登出

•重新登录 - 预期:应用程序启动.发生了什么:应用程序无法启动

msc*_*idt 4

不是完整的解决方案,但也许它有助于调试:

我遵循了本教程,其中描述的步骤与您的非常相似。我还将应用程序存档并导出为开发人员 ID 应用程序,并将其安装到 /Applications 中。

结果:它对我也不起作用。

打开后Console.app显示system.log许多行,如下所示,每 10 秒出现一次:

5 月 24 日 21:18:16 FooBar com.apple.xpc.launchd[1] (my.domain.TestHelper[43372]):无法解析服务指定的 CFBundleIdentifier:-10814:my.domain.TestHelper

即通过works注册helper SMLoginItemSetEnabled,但系统找不到它。手动打开主应用程序并禁用登录时启动功能后,不再显示任何消息。注销似乎也有效。

最后我尝试在 Terminal.app 中手动打开主应用程序:

$ open -b my.domain.Test

它从不同的 Xcode 构建位置和档案中打开了许多旧版本!因此,最好在干净的系统上尝试该功能。否则,启动服务会打开主应用程序或辅助应用程序的意外版本。从 Xcode 测试该功能似乎是不可能的。

Apple还记录了登录项正常工作必须满足的一些条件。

有了一个干净的系统我就可以让它工作了。