用户可以直接从 iOS 访问文件吗?

Can users access files directly from iOS?

我是 iOS 开发的初学者。

我正在通过查看制作文件的示例来复制它。

   override func viewDidLoad() {
        super.viewDidLoad()

        let fileName = "Test"
        let DocumentDirURL =  try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        let fileURL = DocumentDirURL.appendingPathComponent(fileName).appendingPathExtension("")

        let writeString = "write test sting"
        do {
            try writeString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
        } catch _ as NSError {
            print("write error")
        }
    }

我的问题是我当前的代码正在创建文件并将其保存在文档目录中。

  1. 用户能否亲眼看到该文件?
  2. 还有什么我需要有权限才能执行的吗 这个代码?

这是旧的 Swift 代码。如果有代码有更好的方法,请推荐给我。请多多帮助我。提前谢谢你。

这将创建您要保存的目录的路径

 let fileManager = NSFileManager.defaultManager()

    let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)

    if let documentDirectory: NSURL = urls.first as? NSURL {
        // This is where the database should be in the documents directory
        let finalDatabaseURL = documentDirectory.URLByAppendingPathComponent("items.db")

        if finalDatabaseURL.checkResourceIsReachableAndReturnError(nil) {
            // The file already exists, so just return the URL
            return finalDatabaseURL
        } else {
            // Copy the initial file from the application bundle to the documents directory
            if let bundleURL = NSBundle.mainBundle().URLForResource("items", withExtension: "db") {
                let success = fileManager.copyItemAtURL(bundleURL, toURL: finalDatabaseURL, error: nil)
                if success {
                    return finalDatabaseURL
                } else {
                    println("Cant found!")
                }
            } else {
                println("Couldn't find !")
            }
        }
    } else {
        println("Couldn't get documents directory!")
    }