首页 > 解决方案 > 如何简化可可应用程序的安全运行时流程

问题描述

我是 swift 的新手,一般来说是苹果环境的新手,长时间的 windows 用户。我目前的问题是我希望我的应用程序可以在任何 MacOS 中运行,没有任何密码问题,目前在 Xcode 中启动时它会询问我密码,然后按预期工作。(仅适用于 sudo -S,否则不会)

为了达到这一点,我不得不禁用沙盒,不确定这是否是我应该做的,但当时这是我能让应用程序工作的唯一方法。

同样在一些帖子中,我看到在 Mojave 中,建议在此类应用程序中为终端设置全盘访问,所以我这样做了。

另一个问题是如何调整这个应用程序,以便我可以把它交给朋友,他可以在没有任何密码和其他开发人员技巧的情况下使用它。

谢谢。

import Cocoa

class ViewController: NSViewController {
    @IBOutlet weak var txtUrl1: NSTextField!
    @IBOutlet weak var btnDownload: NSButton!

    @IBAction func clickDownload(_ sender: Any) {
        let url = txtUrl1.stringValue
        let command = "sudo -S /usr/local/bin/youtube-dl -f best " + url + " -o '/Users/Matthew/Library/Containers/matthew.youtube/Data/%(title)s.%(ext)s' "
        shell(command)
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    func shell(_ command: String) -> String {
        let task = Process()
        task.launchPath = "/bin/bash"
        task.arguments = ["-c", command]

        let pipe = Pipe()
        task.standardOutput = pipe
        task.launch()

        let data = pipe.fileHandleForReading.readDataToEndOfFile()
        let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String

        return output
    }
    override var representedObject: Any? {

        didSet {

        // Update the view, if already loaded.
        }
    }
}

标签: swiftcocoamacos-mojaveyoutube-dl

解决方案


推荐阅读