已实现的助手应用程序,但未在登录时启动 [英] Implemented helper app but does not launch on login

查看:107
本文介绍了已实现的助手应用程序,但未在登录时启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a need to boot my Mac application when the user logins in.

这就是我所做的:

我创建了一个新的可口可乐应用目标.

I created a new Coca Application Target.

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

• Build Settings set "Skip Install" to "YES"

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

• Info.plist "Application is background only" to "YES"

•在主应用程序和辅助应用程序上启用了沙箱.

• Enabled Sandbox on both main and helper app.

•将复制文件阶段添加到主应用程序:包装器,目录/库/LoginItems,添加了Helper.app

• Added Copy File Phase to main application: Wrapper, Contents/Library/LoginItems, added 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
    }


}

主应用程序的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!)
        }

    }

测试

•Cmd + B构建应用程序

• Cmd + B to build application

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

• Right click .app in Product > Show in Finder

•启动的应用程序

•注销

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

• Log back in -- Expected: Application boots. What Happened: Application Did not boot

推荐答案

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

Not a full solution, but perhaps it helps on debugging:

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

I followed this tutorial which describes steps very similar to yours. I also archived and exported the application as a Developer ID application and installed it into /Applications.

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

Result: it didn't work for me either.

打开Console.app后,system.log会显示很多类似以下内容的行,每10秒出现一次:

After opening Console.app the system.log showed many lines like the following, appearing every 10 seconds:

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

即通过SMLoginItemSetEnabled注册帮助程序是可行的,但是系统找不到它.手动打开主应用程序并禁用登录时启动功能后,不再显示任何消息.取消注册似乎也可以.

I.e. registering the helper via SMLoginItemSetEnabled works, but the system cannot find it. After opening the main app manually and disabling the launch-on-login feature, no more messages showed up. Deregistering also seems to work.

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

At last I tried to open the main app manually in Terminal.app:

$ open -b my.domain.Test

$ open -b my.domain.Test

它打开了来自各种Xcode构建位置和档案的许多旧构建!因此,请尝试使用清洁系统使用该功能.否则,启动服务会打开主应用程序或辅助应用程序的未预期版本.从Xcode测试功能似乎是不可能的.

It opened many old builds from various Xcode build locations and archives! Hence rather try the feature with a clean system. Otherwise the launch services my open unexpected versions of the main or helper apps. Testing the feature from Xcode seems to be impossible.

Apple 还记录了登录项正常运行所必须满足的一些条件.

Apple also documents some conditions that must be met for login items to work.

使用干净的系统,我可以正常工作.

With a clean system I could get it to work.

这篇关于已实现的助手应用程序,但未在登录时启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆