我正在开发一个更新文件/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist
的应用。该文件归root所有。我目前正在使用/usr/libexec/authopen
工具更新它,使用NSTask
和NSPipe
运行该工具。这是代码:
func saveAs(fname:String) {
// Create a dictory to write back out, and replace the knownNetworks and the preferredOrder
let d = NSMutableDictionary(dictionary:airport_preferences)
d[KnownNetworks] = networks
d[PreferredOrder] = preferred_order
let tempFileName = NSTemporaryDirectory() + "/preferences.new"
d.writeToFile(tempFileName,atomically:true)
let data = NSData(contentsOfFile: tempFileName)
let task = NSTask()
task.launchPath = "/usr/libexec/authopen"
task.arguments = ["-c","-w",fname]
let pipe = NSPipe()
task.standardInput = pipe
task.launch()
let old_signal = signal(SIGPIPE,SIG_IGN)
pipe.fileHandleForWriting.writeData(data!)
pipe.fileHandleForWriting.closeFile()
task.waitUntilExit()
signal(SIGPIPE,old_signal)
do {
try NSFileManager.defaultManager().removeItemAtPath(tempFileName)
} catch let error as NSError {
print(error.description)
}
}
要移至App Sandbox,我可以让用户在浏览器中选择该文件来打开该文件。这并不难,因为我可以预先定位NSOpenPanel
。 Mac App Sandboxing- Updating files outside the sandbox描述了如何更新沙箱外的文件,但不是root所拥有的文件。
当我在沙盒下运行我的代码时,我收到此错误:
tempfile: /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist length: 339710
arguments: Optional(["-c", "-w", "/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist"])
2015-09-26 11:46:07.699 WiFi Editor[94175:2887221] An uncaught exception was raised
2015-09-26 11:46:07.700 WiFi Editor[94175:2887221] Couldn't posix_spawn: error 1
2015-09-26 11:46:07.703 WiFi Editor[94175:2887221] (
(该代码仍然包含我的调试打印语句。)
那么如何从沙箱更新系统文件?
答案 0 :(得分:1)
我担心沙盒上的应用程序根本无法实现这一点。不允许用户将文本粘贴到终端窗口中。