菜鸟教程小白 发表于 2022-12-11 19:07:45

ios - 如何在 Swift 中将文件写入位于 Apple Files App 中的文件夹


                                            <p><p>我的 Xcode 项目中有一个 XML 文件,我首先尝试将其保存到磁盘,其次如何判断我是否已成功保存它?这是正确的方法吗?使用模拟器,我导航到 iOS 11 中的新"file"文件夹,但我没有看到它,但我不确定它是否应该在那里? </p>

<pre><code>guard let path = Bundle.main.url(forResource: &#34;sample&#34;, withExtension: &#34;xml&#34;) else {print(&#34;NO URL&#34;); return}
    let sample = try? Data(contentsOf: path)


print(&#34;sample XML = \(String(describing: sample?.debugDescription))&#34;)

//put xml file on the device
let filename = getDocumentsDirectory().appendingPathComponent(&#34;sample.xml&#34;)
do {
    try sample?.write(to: filename)
} catch {
    print(&#34;ERROR&#34;)
}
</code></pre>

<p>更新以包括我检查文件是否存在:</p>

<pre><code> //check if file exists
    let checkPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as String
    let url = URL(fileURLWithPath: checkPath)
let filePath = url.appendingPathComponent(&#34;sample.xml&#34;).path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
    print(&#34;FILE AVAILABLE&#34;)
} else {
    print(&#34;FILE NOT AVAILABLE&#34;)
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以使用 <code>UIDocumentInteractionController</code> 并让用户在您分享您的网址时选择他想要保存您的文件的位置。用户只需要选择保存到文件并选择保存要导出的文件的目录即可。</p>
<p>您可以使用 <code>UIDocumentInteractionController</code> 共享位于您的 App 包内、您的 Documents 目录或可从您的 App 访问的其他文件夹中的任何文件类型。</p>
<pre><code>class ViewController: UIViewController {
    let documentInteractionController = UIDocumentInteractionController()
    func share(url: URL) {
      documentInteractionController.url = url
      documentInteractionController.uti = url.typeIdentifier ?? &#34;public.data, public.content&#34;
      documentInteractionController.name = url.localizedName ?? url.lastPathComponent
      documentInteractionController.presentOptionsMenu(from: view.frame, in: view, animated: true)
    }
    @IBAction func shareAction(_ sender: UIButton) {
      guard let url = URL(string: &#34;https://www.ibm.com/support/knowledgecenter/SVU13_7.2.1/com.ibm.ismsaas.doc/reference/AssetsImportCompleteSample.csv?view=kc&#34;) else { return }
      URLSession.shared.dataTask(with: url) { data, response, error in
            guard let data = data, error == nil else { return }
            let tmpURL = FileManager.default.temporaryDirectory
                .appendingPathComponent(response?.suggestedFilename ?? &#34;fileName.csv&#34;)
            do {
                try data.write(to: tmpURL)
                DispatchQueue.main.async {
                  self.share(url: tmpURL)
                }
            } catch {
                print(error)
            }

      }.resume()
    }
}
</code></pre>
<hr/>
<pre><code>extension URL {
    var typeIdentifier: String? {
      return (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier
    }
    var localizedName: String? {
      return (try? resourceValues(forKeys: [.localizedNameKey]))?.localizedName
    }
}
</code></pre>
<hr/>
<p><strong>编辑/更新</strong>:</p>
<p>如果您想公开位于 App Bundle 文档目录中的文件,您可以查看这篇文章 <a href="https://stackoverflow.com/a/70613710/2303865" rel="noreferrer noopener nofollow">How can I display my App documents in the Files app for iPhone</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 Swift 中将文件写入位于 Apple Files App 中的文件夹,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46972013/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46972013/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 Swift 中将文件写入位于 Apple Files App 中的文件夹