iossharinguiactivityviewcontrolleruiapplicationdelegateuti

Receiving files from other iOS apps with Copy In


I would like my app to receive .wav files from the files.app app. I added com.microsoft.waveform-audio to the Document Types array in Info.plist, and my app shows up in the share action sheet as “Open In X” ( not Copy In ). I want to actually copy the files in, not open them in place, and the os will not allow me to copy these (error code 257). I’m trying to copy the file in the UIApplicationLaunchOptionsURLKey Path, but no go.

The launchOptions dictionary contains a RequireOpenInPlace=1 as well, in the applicationDidLaunch WithOptions.

How do I let other apps copy files to mine with the UIActivityViewController?


Solution

  • If you are opening the file "in place" (not copying) you should make the resource referenced by the url accessible to the process.

    Try calling url.startAccessingSecurityScopedResource() before accessing the file. Then you should be able to copy the file.

    Don't forget to balance the call with url.stopAccessingSecurityScopedResource() when you don't need the file anymore.

    Reference: https://developer.apple.com/documentation/foundation/nsurl#1663809

    Edit: Another solution might be if you set LSSupportsOpeningDocumentsInPlace to NO in your Info.plist. Then you would always see "Copy to XYZ" in share sheet and so will system copy the file to your Documents/Inbox folder in your app sandbox.