How can I create an app icon folder in the files app and copy items to it?

967 Views Asked by At

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?

1

There are 1 best solutions below

11
On

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.