简体   繁体   中英

How do I move a text file I exported from app data to a place outside the app's sandbox using Swift/SwiftUI in iOS?

I have an app that normally stores data for the user. All works fine but I want to give the user the chance to export the data to a CSV and do as they wish with it.

Right now, I have the CSVs but they sit inside the app's sandbox and are very difficult and unfriendly for the user. How can I move these to a place outside the app sandbox to a place that the user is more familiar with like their Documents folder in iCloud or their Downloads folder?

SwiftUI solution would be great but Swift works too!

I have no code to show since I'm completely stumped.

Thanks @Paulw11 for the direction! I was quite uncertain how to go about it but it's brilliantly simple!

As I created the export files, I saved the URLs into an array. I then passed them to a struct I made using UIDocumentPickerViewController implementing UIViewControllerRepresentable (for SwiftUI).

The struct looks like this:

import SwiftUI
import UIKit

struct DocumentPicker: UIViewControllerRepresentable {
    var URLs: [URL]
    func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentPicker>) ->
        UIDocumentPickerViewController {

        let picker = UIDocumentPickerViewController(urls: URLs, in: .moveToService)
        return picker
}

    func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: UIViewControllerRepresentableContext<DocumentPicker>){}
}

You end up with all your files ready to be moved to a location chosen by the user:

显示的模态窗口允许用户选择保存位置!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM