二、添加代码

在AppDelegate或有设置按钮的地方添加代码

    @IBAction func appWhenLoginAction(_ sender: NSButton) {
        let state = sender.state
        if state == .on {
            self.startupAppWhenLogin(startup: true)
        } else {
            self.startupAppWhenLogin(startup: false)
    func startupAppWhenLogin(startup: Bool) {
        // 这里请填写你自己的Heler BundleID
        let launcherAppIdentifier = "liwei.LwWallpapersHelper"
        // 开始注册/取消启动项
        if SMLoginItemSetEnabled(launcherAppIdentifier as CFString, startup) {
            if startup {
                NSLog("添加登录项成功.")
            } else {
                NSLog("移除登录项成功.")
        } else {
            NSLog("添加失败.")
复制代码

在添加的target的AppDelegate添加代码

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        var compoents = (Bundle.main.bundlePath as NSString).pathComponents as NSArray
        compoents = compoents.subarray(with: NSMakeRange(0, compoents.count - 4)) as NSArray
        let path = NSString.path(withComponents: compoents as! [String])
        NSWorkspace.shared.launchApplication(path)
        terminate()
    @objc func terminate() {
        NSApp.terminate(nil)
复制代码

然后导出APP运行一下,就可以了。

土豆崽xxx macOS
私信