问题
I have an OS X app, which stores an app-scoped bookmark to persist access to certain directories. I am able to write to those directories without any problems, but there is a part in my code where I want to make an extra check to confirm that the path is writable and it fails.
var fileManager: NSFileManager = NSFileManager.defaultManager()
var baseUrl: NSURL = try! NSURL(byResolvingBookmarkData: data, …)
var fileUrl: NSURL = url.URLByAppendingPathComponent("foo.txt")
// Changing this order has no effect, I make only the first start access call,
// the second one is to demonstrate that it's not working.
baseUrl.startAccessingSecurityScopedResource() // true
fileUrl.startAccessingSecurityScopedResource() // false
// File manager confirms that base path is writable, but anything inside
// it – not. This worked perfectly fine before sandboxing the app.
fileManager.isWritableFileAtPath(baseUrl.path!) // true
fileManager.isWritableFileAtPath(fileUrl.path!) // false
// Writing file to the `fileUrl` works anyway, even though
// file manager thinks it's not writable.
writeMyFile(toUrl: fileUrl)
// Here's another interesting observation, is writable starts working
// once the files get created.
fileManager.isWritableFileAtPath(fileUrl.path!) // false
fileManager.createFileAtPath(fileUrl.path!, contents: nil, attributes: nil)
fileManager.isWritableFileAtPath(fileUrl.path!) // true
Am I doing something wrong or is there a problem with file manager inside a sandbox? Is there a decent way to check if subpath is writable, i.e., without creating files?
来源:https://stackoverflow.com/questions/35307603/check-if-path-under-app-scoped-bookmark-is-writable-inside-sandboxed-app