Search code examples
iosicloudicloud-drive

iOS11: Can't create folder again in iCloud drive once deleted manually


I have developed an app for saving a file to iCloud drive from the app.

I am saving a file from my app to iCloud drive. So its working as expected. Its creating my app folder with saved file in iCloud drive app. But if I delete the folder and try to save the file again from my app,

its giving an error as below :

"file.txt" couldn't be moved to "Documents" because either the former doesn't exist, or the folder containing the later doesn't exist.

This issue I'm facing only in iOS11. In iOS 10 its working properly.

How to fix this issue. Thanks in advance.


Solution

  • I found that you need to re-create the folder if it gets deleted, it won't create it automatically. So I'd try this:

    let fileManager = FileManager.default
    let iCloudPath = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("DirectoryName")
    
    do { 
        // Try to create the file here
    } catch let error {
        // If that operation fails, you print the error and create the folder again
        print(error.localizedDescription)
    
        do { 
            try fileManager.createDirectory(atPath: iCloudPath!.path, withIntermediateDirectories: true, attributes: nil) }
        catch let error { 
            print("Unable to create directory \(error.localizedDescription)") }
    }