'How can I select files using file importer in SwiftUI?

I want to select file from FileImporter in SwiftUI, but i can't select files

enter image description here

this is my code:

struct ContentView: View {

@State var isShowing = false

var body: some View {
    
    VStack {
        Button {
            isShowing.toggle()
        } label: {
            Text("documents")
        }.fileImporter(isPresented: $isShowing, allowedContentTypes: [.item]) { result in
            
            switch result {
            case .success(let Fileurl):
                print(Fileurl)
            case .failure(let error):
                print(error)
            }     
        }
    }  
}

how can i fix that?



Solution 1:[1]

After a very long search, I found the answer to this question

It is not possible to select a file in the simulator But if you need to select files to test your code you can do this steps

Step 1: Hold on file

Step 2: When the file pops up, click on it again

With these two steps you can select your file

Solution 2:[2]

Here what you looking for:

struct ContentView: View {
    
    @State var isShowing = false
    
    var body: some View {
        
        VStack {
            Button {
                isShowing.toggle()
            } label: {
                Text("documents")
            }
            .fileImporter(isPresented: $isShowing, allowedContentTypes: [.item], allowsMultipleSelection: true, onCompletion: { results in
                
                switch results {
                case .success(let fileurls):
                    print(fileurls.count)
                    
                    for fileurl in fileurls {
                        print(fileurl.path)
                    }
                    
                case .failure(let error):
                    print(error)
                }
                
            })

        }
        
    }
    
}

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amin Rezaew
Solution 2