2

Best way to use iCloud Documents Storage

I succeeded in uploading a file from my app to icloud by referring to the link above.

But I can't upload files since I deleted the app and reinstalled it

Below is the error log

Error Domain=NSCocoaErrorDomain Code=512 "Could not copy 'backup.db' to 'Documents'." UserInfo={NSSourceFilePathErrorKey=/var/mobile/Containers/Data/Application/7B27D1E4-FE7F-4541-8E04-68CCB2A894D4/Documents/backup.db, NSUserStringVariant=(
    Copy
), NSDestinationFilePath=/private/var/mobile/Library/Mobile Documents/iCloud~com~sinwho~CheckDay/Documents/backup.db, NSFilePath=/var/mobile/Containers/Data/Application/7B27D1E4-FE7F-4541-8E04-68CCB2A894D4/Documents/check_day.db, NSUnderlyingError=0x2804517d0 {Error Domain=NSPOSIXErrorDomain Code=20 "Not a directory"}}

Try adding the following to Info.plist

<key>NSUbiquitousContainers</key>
<dict>
    <key>iCloud.com.sinwho.CheckDay</key>
    <dict>
        <key>NSUbiquitousContainerIsDocumentScopePublic</key>
        <true/>
        <key>NSUbiquitousContainerName</key>
        <string>MyApp</string>
        <key>NSUbiquitousContainerSupportedFolderLevels</key>
        <string>Any</string>
    </dict>
</dict>

I also tried startAccessingSecurityScopedResource. No error occurs, but the file is not actually copied.

I also changed the CFBundleVersion

Is there any way to solve this problem?

if DocumentsDirectory.iCloudDocumentsURL!.startAccessingSecurityScopedResource() {
                        try fileManager.copyItem(at: DocumentsDirectory.dbFileURL.appendingPathComponent(file), to:
                                                    DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(file))      
}
DocumentsDirectory.iCloudDocumentsURL!.stopAccessingSecurityScopedResource()
Hooman
  • 114
  • 1
  • 1
  • 12
ppol2918
  • 27
  • 4
  • It looks like you have tried to save the full URL of the backups db? You can't do that. – matt Jul 11 '21 at 07:02
  • matt thanks for the reply But it doesn't seem to be a url problem. Create a new project and run it with the same code you can upload files to icloud The problem occurs when reinstalling the app after uninstalling it. – ppol2918 Jul 11 '21 at 07:07
  • @ppol2918 Yes, because as matt said, you have saved the full URL (including the container path) and when you uninstall then reinstall the app, *the container is not the same anymore*. It's better to ask the system to give you the current container URL and just save the filename (and a subfolder if you need that) without the full path. – Eric Aya Jul 11 '21 at 13:57

0 Answers0