例如:在KillerDemo 程序中 kill 掉 DemoA 程序
- 测试代码
@IBAction func killAction(_ sender: Any) {
let runingApps = NSRunningApplication.runningApplications(withBundleIdentifier: targetIDField.stringValue)
runingApps.forEach { (app) in
let bSuc = app.terminate()
// let bSuc = kill(app.processIdentifier, SIGKILL)
let printInfo = bSuc ? "kill app[\(app.bundleIdentifier ?? "")] sucessfullly" : "kill app[\(app.bundleIdentifier ?? "")] failed"
print(printInfo)
}
}
1. KillerDemo 启用 sandbox时,无法直接 kill 掉别的进程
1.1 Case 1: KillerDemo 启用 sandbox , DemoA 未启用 sandbox
需要在 .entitlements 文件中配置信息。
在没有配置信息之前,是无法结束进程的。func kill(_: pid_t, _: Int32) -> Int32
也不行。
使用脚本,我没有试过,不过猜测也不行。
添加配置信息
com.apple.security.get-task-allow
,com.apple.security.temporary-exception.apple-events
entitlements 如下:
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.get-task-allow</key>
<true/>
<key>com.apple.security.temporary-exception.apple-events</key>
<array>
<string>com.apple.TextEdit</string>
</array>
</dict>
</plist>
只要在com.apple.security.temporary-exception.apple-events
字段中添加的 bundle identifier
都可以 kill 掉了
1.1 Case 2: KillerDemo 启用 sandbox , DemoA也启用 sandbox
寻找方法中…