1
votes

In my app, I can use

let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["com.xxxx.yyyy.zzz"], in: UIDocumentPickerMode.import)

Just fine to create a document picker. However when I try to export a document using .exportToService:

let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(url: URL(string: kUTTypeText as String)!, in: UIDocumentPickerMode.exportToService)

to be able to export the data from my app, I get a stream of error messages:

[DocumentManager] Failed to create FPSandboxingURLWrapper for public.text. Error Domain=NSPOSIXErrorDomain Code=1 "couldn't issue sandbox extension com.apple.app-sandbox.read for 'public.text': Operation not permitted" UserInfo={NSDescription=couldn't issue sandbox extension com.apple.app-sandbox.read for 'public.text': Operation not permitted}

I am unsure which sandbox settings are wrong as I can directly write to files just fine with my app, just the UIDocumentPickerViewController is not working.

1

1 Answers

0
votes

For .exportToService to work you should specify a URL of the file you want to export.

Example for a newly created .txt file:

    let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let fileURL = URL(fileURLWithPath: "myFile", relativeTo: directoryURL).appendingPathExtension("txt")
    
    let myString = "string"
    guard let data = myString.data(using: .utf8) else { return }
    try? data.write(to: fileURL)

    let exportMenu = UIDocumentPickerViewController(url: fileURL, in: .exportToService)

    exportMenu.delegate = self
    exportMenu.modalPresentationStyle = .formSheet
   
    present(exportMenu, animated: true)

Result - file saved to iCloud: export result

(To be able to save files to iClould you need it in Capabilities and check "Key-value storage")