检查应用程序范围书签下的路径是否可在沙盒应用程序内写入

时间:2016-02-10 05:34:35

标签: objective-c swift security sandbox appstore-sandbox

我有一个OS X应用程序,它存储应用程序范围的书签以持久访问某些目录。我能够毫无问题地写入这些目录,但是我的代码中有一部分需要进行额外检查以确认路径是否可写且失败。

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

我做错了什么或沙箱中的文件管理器有问题吗?有没有一种可行的方法来检查子路径是否可写,即没有创建文件?

0 个答案:

没有答案