0

I am trying to use iCloud with SwiftUI. I just want to be able to read/write json files stored there. I believe I have taken all the proper steps to get there, including the necessary changes to Signings & Capabilities and the addition of settings in info.plist. I have set up iCloud on the simulator. But the following code always returns nil for the URL:

 let driveURL = FileManager.default .url(forUbiquityContainerIdentifier:  
        nil)?.appendingPathComponent("Documents")
                if driveURL != nil {
                        print("iCloud available")
                        let fileURL = driveURL!.appendingPathComponent("test.txt")
                        try? "Hello word".data(using: .utf8)?.write(to: fileURL)
                    } else {
                        print("iCloud not available")
                    }

What am I missing?

M. Thomas
  • 1
  • 2
  • Have you got the required item in entitlement? – Ptit Xav Feb 27 '22 at 13:06
  • I don't know what you mean. Sorry, but I'm a newbie. Can you explain? – M. Thomas Mar 03 '22 at 22:31
  • iCloud has been added to the project and the container has iCloud ability, if that is what you are talking about. Also, iCloud was added to the simulator. – M. Thomas Mar 04 '22 at 01:50
  • From the documentation, you must call it once before using it. This enable the system to create the app sandbox directory on iCloud if it does not exists. The default containerId is the first listed in entitlement array. – Ptit Xav Mar 04 '22 at 06:32
  • Obtaining the URL twice does not seem to help. – M. Thomas Mar 07 '22 at 03:29

0 Answers0