Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

441
Views
Obtenga la URL del cuadro de diálogo Abrir de la aplicación estándar basada en documentos de Swift

Estoy tratando de extraer la URL del documento que un usuario ha seleccionado en el cuadro de diálogo "Abrir" predeterminado de mi aplicación macOS basada en documentos. Entiendo, se pasa un fileWrapper al método init, pero ¿hay alguna forma de extraer la ruta/URL de dicho contenedor?

Gracias,

Lars

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

FileWrapper tiene un campo de nombre de filename , por lo que probablemente lo usaría.

over 4 years ago · Santiago Trujillo Report

0

El panel abierto le proporciona la URL si alguien hace clic en el botón Abrir (Aceptar). NSOpenPanel tiene una propiedad urls que contiene las URL de los archivos seleccionados.

Los importadores de archivos de SwiftUI le brindan una URL si la apertura fue exitosa.

 .fileImporter(isPresented: $isImporting, allowedContentTypes: [.png, .jpeg, .tiff], onCompletion: { result in switch result { case .success(let url): // Use the URL to do something with the file. case .failure(let error): print(error.localizedDescription) } })

ACTUALIZAR

El panel de apertura de documentos de SwiftUI funciona de manera diferente al importador de archivos. Podría intentar trabajar con NSOpenPanel directamente. El siguiente artículo debería ayudar:

Guardar y abrir paneles en aplicaciones macOS basadas en SwiftUI

over 4 years ago · Santiago Trujillo Report

0

La única forma en que pude hacer que esto funcionara fue agregar un inicializador personalizado a la vista de contenido que vuelve a escribir la URL en el documento. Muy poco elegante, pero aquí estamos:

aplicación:

 import SwiftUI @main struct FileOpenApp: App { var body: some Scene { DocumentGroup(newDocument: FileOpenDocument()) { file in ContentView(document: file.$document, fileURL: file.fileURL) } } }

Documento:

 struct FileOpenDocument: FileDocument { var text: String var originalFilePath: String var firstOpen: Bool init(text: String = "Hello, world!") { self.text = text self.firstOpen = true self.originalFilePath = "NewFile Path" } static var readableContentTypes: [UTType] { [.exampleText, .CSVtext, .plainText] } init(configuration: ReadConfiguration) throws { guard let data = configuration.file.regularFileContents, let string = String(data: data, encoding: .utf8) else { throw CocoaError(.fileReadCorruptFile) } text = string self.firstOpen = true self.originalFilePath = "ReadPath" } func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper { let data = text.data(using: .utf8)! return .init(regularFileWithContents: data) } }

Vista de contenido:

 struct ContentView: View { @Binding var document: FileOpenDocument var documentURL: URL? init(document: Binding<FileOpenDocument>, fileURL: URL?){ self._document = document self.documentURL = fileURL if document.firstOpen.wrappedValue == true { if let path = self.documentURL?.path { self.document.originalFilePath = path document.firstOpen.wrappedValue = false } } } var body: some View { TextEditor(text: $document.text) Text("ContentView Path: \(self.documentURL?.path ?? "no Document Path")") Text("Document Path: " + document.originalFilePath) } }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!