File Export from iOS - eventually import too

iOS 15 - iOS 26.x, SwiftUI, Xcode 26.1.1 (rewritten without links)

I’m attempting to add the capability to export a file from CoreData.

Since ShareLink requires iOS16+, I found an article showing how to create a UIKit wrapper for 'UIActivityViewController' This does work, but I end up with multiple files instead of one. How do I merge this into one file?

Note: I don’t think I want to write to the app’s document directory – rather I’d like to allow the user to export to iCloud. Eventually, I’d like to share a list of dates with descriptions and if someone puts a sensitive date in there – I don’t want it to be left in a directory. I would include a warning that the file that they export needs to be handled with care if there are any sensitive dates. Some might consider birthdays as private information, right?

I'd also like to be able to name the file - so far when the share sheet comes up, I select "Save to Files" and it uses a default name of text.txt

My next step is to be able to import a CSV file (roundtrip) - could you point me to resources for how to do that?

Answered by DTS Engineer in 880189022

No, you don't need to write the data as a file to your the app’s document directory. With UIActivityViewController, you can share a piece of text, as shown in the following code example

let text =  "This is a piece of text."
let activityViewController = UIActivityViewController(activityItems: [text], applicationActivities: nil)

With that, when activityViewController is presented, it shows the system-provided share UI that contains a Save to Files button, which allows you to save the text as a file to iCloud Drive. Tapping the button shows a UI that has a Save as field for you to input the file name for the taget file.

To import a file, consider using SwiftUI fileImporter.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

No, you don't need to write the data as a file to your the app’s document directory. With UIActivityViewController, you can share a piece of text, as shown in the following code example

let text =  "This is a piece of text."
let activityViewController = UIActivityViewController(activityItems: [text], applicationActivities: nil)

With that, when activityViewController is presented, it shows the system-provided share UI that contains a Save to Files button, which allows you to save the text as a file to iCloud Drive. Tapping the button shows a UI that has a Save as field for you to input the file name for the taget file.

To import a file, consider using SwiftUI fileImporter.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks for these answers, but I'm afraid I need a bit more help. How do you go about presenting the activityViewController? Could you show this in a small example app?

I had previously written this and it does share but it creates 2 files, and doesn't present any way to rename the file.

import SwiftUI
import UIKit

struct ContentView: View {
    @State private var showShareSheet = false
    @State private var textItem1 = ""
    @State private var textItem2 = ""
    
//    @State private var shareItems: [Any] = [
//      "Check out this file!",
//      URL(fileURLWithPath: FileManager.documentsDirectory.path()) // Replace with a real file URL
//    ]
    
var shareItems: [Any] {
    [   textItem1,
          textItem2
//          URL(fileURLWithPath: "TinySavedItems")
        ]
}
    var body: some View {
        VStack {
            TextField("enter text", text: $textItem1)
                .padding()
            TextField("enter text", text: $textItem2)
                .padding()
          
            Button("Share", systemImage: "square.and.arrow.up") {
              showShareSheet = true
            }
            .sheet(isPresented: $showShareSheet) {
                ActivityViewController(activityItems: shareItems)
            }
        }
    }
    
    struct ActivityViewController: UIViewControllerRepresentable {
        
        // Items to share (e.g., text, URLs, files)
        let activityItems: [Any]
        // Custom activities (optional)
        let applicationActivities: [UIActivity]? = nil
        // Completion handler (optional)
        @Environment(\.dismiss) var dismiss
        
        // Create the UIActivityViewController
        func makeUIViewController(context: Context) -> UIActivityViewController {
            
            let vc = UIActivityViewController(
                activityItems: activityItems,
                applicationActivities: applicationActivities
            )
            vc.excludedActivityTypes = [
                .postToFacebook, .postToTwitter
            ]
            // Handle completion (e.g., dismiss after sharing)
            vc.completionWithItemsHandler = { _, success, _, error in
                if success || error != nil {
                    dismiss() // Close the sheet after sharing
                }
            }
            return vc
        }
        func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {} //doesn't do anything but is required
    }
}
File Export from iOS - eventually import too
 
 
Q