简体   繁体   English

如何启动外部流程?

[英]How to launch an external Process?

I am reading through the docs of swift and came across Type Methods. 我正在阅读swift的文档并遇到了类型方法。 For example here: https://developer.apple.com/documentation/foundation/process 例如: https//developer.apple.com/documentation/foundation/process

The offered Type Method is: 提供的类型方法是:

class func run(URL, arguments: [String], terminationHandler: ((Process) -> Void)? = nil)

How can I use this in my code? 我如何在我的代码中使用它? For example when I press a button? 例如,当我按下按钮? How can I add a clean-up function to the terminationHandler? 如何向terminationHandler添加清理功能?

In a macos app, you may use run for launching external processes, an example might be: macos应用程序中,您可以使用run来启动外部进程,例如:

1) one-shot execution: 1)一次性执行:

let url = URL(fileURLWithPath:"/bin/ls")
do {
   try Process.run(url, arguments: []) { (process) in
      print("\ndidFinish: \(!process.isRunning)")
   }
} catch {}

2) you may want to use a Process instance to be able to setup more comfortably its behaviour, doing so: 2)您可能希望使用Process实例来更舒适地设置其行为,这样做:

let process = Process()
process.executableURL = URL(fileURLWithPath:"/bin/ls")
process.arguments = ["-la"]
process.terminationHandler = { (process) in
   print("\ndidFinish: \(!process.isRunning)")
}
do {
  try process.run()
} catch {}

So I did launch the ls command (you may check your console for the result), then in the closure terminationHandler I'm getting back such process. 所以我确实启动了ls命令(你可以检查你的控制台的结果),然后在关闭的terminationHandler我得到了这样的过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何快速从外部应用程序启动 yandex 地图应用程序? - How can I launch yandex maps application from external app in swift? 将命令行工具作为分离的进程启动 - Launch Command Line Tool as detached Process SwiftUI 预览错误(进程启动失败) - SwiftUI Preview error (process launch failed) 如何启动 ViewController - how to launch ViewController 如何显示启动图像 - How to display launch image Xcode10.3 中的设备调试问题“iPhone 拒绝了启动请求。内部启动错误:进程启动失败:未指定” - Trouble with device debugging in Xcode10.3 "iPhone has denied the launch request. Internal launch error: process launch failed: Unspecified" 无法启动应用,“进程启动失败:没有这样的文件或目录” - Can't start app, “process launch failed: No such file or directory” Swift / Xcode:WebProcessProxy 连接标识符无效(Web 进程启动失败) - Swift / Xcode: WebProcessProxy Invalid connection identifier (web process failed to launch) 为什么我的Swift Process()启动路径吐出“启动路径不可访问”? - Why is my Swift Process() launch path spitting out `launch path not accessible`? 如何使用UIImagePickerControllerDelegate在viewDidLoad上启动相机? - How to launch camera on viewDidLoad with UIImagePickerControllerDelegate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM