2015-09-26 52 views
1

我正在开发一个更新文件/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist的应用程序。该文件由root拥有。我目前正在使用/usr/libexec/authopen工具更新它,运行工具NSTaskNSPipe。这里是代码:从沙盒应用程序更新系统文件

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) 
    } 
} 

要移动到应用程序沙箱,我可以通过让用户在浏览器中选择它来打开该文件。这并不难,因为我可以预先定位NSOpenPanelMac 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

添加此为答案,我会接受它。我的计划是显示一个窗口,其中需要将文本粘贴到终端窗口中。这是允许的吗? – vy32

回答

1

恐怕对于沙盒应用程序来说,这是不可能的。不允许用户将文本粘贴到终端窗口中。