SwiftUI - модификатор .fileImporter для загрузки файлов USD

Цель: загрузить файлы в долларах США с помощью модификатора SwiftUI .fileImporter.

Согласно документации Apple, USD - это поддерживаемый тип uttype с модификатором .fileImporter. https://developer.apple.com/documentation/uniformtypeidentifiers/uttype . https://developer.apple.com/documentation/uniformtypeidentifiers/uttype/3551587-usd

Я попробовал приведенный ниже код, но он не работает. Кому-нибудь удалось заставить это работать?

      import SwiftUI

struct ContentView: View {
    
    @State var openFile = false
    @State var usdFile = Data()

    @State var fileName = ""

    
    var body: some View {
    

        Form {
            
            //import USD
            Button(action: {
                self.openFile.toggle()
            }){
                
                Text(fileName)
            
            }
            


        }
        
        .navigationTitle("File Importer")


        //file importer
        .fileImporter(isPresented: $openFile, allowedContentTypes: [.usd]) { (res) in
            

            
            do{
                let fileUrl = try res.get()
                print(fileUrl)
                
                self.fileName = fileUrl.lastPathComponent
                print(fileName)

                
                if let usdFile = try? Data(contentsOf: fileUrl),
           
                fileUrl.stopAccessingSecurityScopedResource()
                
            } catch{
                
                print ("error reading")
                print (error.localizedDescription)
            }
            
        }
        
    }
}

0 ответов

Другие вопросы по тегам