TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Document Picker

Forums > SwiftUI

My first attempt to prompt user for, and read in a text file. Does not request a file. I have entered the usage descriptions for Privacy Documents, desktop and downloads in the info tab. Is there a different Privacy setting I need perhaps?

import SwiftUI
import MobileCoreServices
import UniformTypeIdentifiers

struct FileImporterView: UIViewControllerRepresentable {
    @Binding var fileURL: URL?
    @Environment(\.presentationMode) var presentationMode

    func makeCoordinator() -> Coordinator {

    func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
        let picker = UIDocumentPickerViewController(forOpeningContentTypes: [.commaSeparatedText])
        picker.allowsMultipleSelection = false
        picker.delegate = context.coordinator
        return picker

    func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: Context) {
        // No update needed

    class Coordinator: NSObject, UIDocumentPickerDelegate {
        let parent: FileImporterView

        init(_ parent: FileImporterView) {
            self.parent = parent

        func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
            parent.fileURL = urls.first

        func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

struct ImportSkippers: View {
    @State private var fileURL: URL?

    var body: some View {
        VStack {
            Text("Selected File: \(fileURL?.lastPathComponent ?? "None")")

            Button("Import Text File") {
                fileURL = URL(string: "start") // Reset fileURL

                let fileImporter = FileImporterView(fileURL: $fileURL)


Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.