我有一个 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
我做错了什么或者沙箱内的文件管理器有问题吗?有没有一种不错的方法来检查子路径是否可写,即不创建文件?
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)