0

I want to transfer some files (sound, texts, etc.) from an iOS app to the files app. In addition, I want to put all these items into a folder which has the same name as my app - as it is the case with GarageBand or KeyNote, for example.

In Xcode, I did enable the iCloud Documents capability - I did also define a Container "iCloud.xxx.yyy" - see code below.

guard let fileURL = Bundle.main.url(forResource: "test", withExtension: "aiff") else { return }

guard let containerURL = FileManager.default.url(forUbiquityContainerIdentifier: "iCloud.xxx.yyy") else { return }

if !FileManager.default.fileExists(atPath: containerURL.path) {
    try FileManager.default.createDirectory(at: containerURL, withIntermediateDirectories: true, attributes: nil)
}

let backupFileURL = containerURL.appendingPathComponent("test.aiff")
if FileManager.default.fileExists(atPath: backupFileURL.path) {
    try FileManager.default.removeItem(at: backupFileURL)
    try FileManager.default.copyItem(at: fileURL, to: backupFileURL)
} else {
    try FileManager.default.copyItem(at: fileURL, to: backupFileURL)
}

When I run my code, it seems to work - anyhow, I can't see nor folder representing my app name, nor "test.aiff" file in the files app. What is wrong with my approach?

TylerP
  • 9,600
  • 4
  • 39
  • 43
Ulrich Vormbrock
  • 369
  • 2
  • 13

1 Answers1

1

You don't need to copy/move any file. What you need is to allow your app documents to be accessible from the other apps. Just go to your Info plist and allow "Supports Document Browser". All documents in your Documents directory will be automatically available there.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • Thank you for your hint! I did rectify the info.plist file - anyhow, no items of my app's document folder appear in the files app. Maybe the transfer takes some time? – Ulrich Vormbrock Jan 19 '21 at 16:48
  • AFAIR You need to define which documents types you want to support in your app. – Leo Dabus Jan 19 '21 at 16:50
  • Try signing off iCloud and signing on again. Are you testing it on a real device or simulator? – Leo Dabus Jan 19 '21 at 16:57
  • both on simulator, both on iPhone – Ulrich Vormbrock Jan 19 '21 at 16:58
  • All I have on my app is this setting to show the app folder inside Files > On My Phone directory and UIFileSharingEnabled "Application supports iTunes file sharing" to make then appear in the files tab of the device. – Leo Dabus Jan 19 '21 at 16:59
  • I also have the Document Types set to public.data, public.content, public.composite-​content, public.plain-text, public.image, public.audio, public.movie, com.adobe.pdf. I dont remember setting anything else to make it work. – Leo Dabus Jan 19 '21 at 17:00
  • https://www.dropbox.com/s/uv7zijnfu1ar37a/On%20My%20Phone.jpg?dl=1 – Leo Dabus Jan 19 '21 at 17:03
  • 1
    Thank you Leo! Now I did reset all contents on the simulator and login to iCloud. Now I can find my app folder with some files such as sound and sqlite files under "on my iPhone". I'm not sure if they'll appear also in the iCloud Drive - maybe it'll take some time. – Ulrich Vormbrock Jan 19 '21 at 17:15
  • @UlrichVormbrock I dont think they will appear there if you can not see them now. I dont know what you are expecting to see on iCloud. My post was about having the app folder show inside OnMyPhone directory inside Files App. – Leo Dabus Jan 19 '21 at 17:19
  • seeing my files on iCloud is only a "nice to have". Maybe it can be useful if the user is able to edit a text file on his iMac and import it later back into the iPhone app – Ulrich Vormbrock Jan 19 '21 at 17:25
  • You can allow the user to export/save the file from your app to iCloud but the user will have to choose where he wants to save it. – Leo Dabus Jan 19 '21 at 19:25