How to write a file to iCloud Drive Documents folder?

How to write a file to iCloud Drive Documents folder?

I still have no luck to see the file.


my info.plist looks like below:

<key>NSUbiquitousContainers</key>

<dict>

<key>iCloud.$(PRODUCT_BUNDLE_IDENTIFIER)</key>

<dict>

<key>NSUbiquitousContainerIsDocumentScopePublic</key>

<true/>

<key>NSUbiquitousContainerName</key>

<string>$(PRODUCT_NAME)</string>

<key>NSUbiquitousContainerSupportedFolderLevels</key>

<string>Any</string>

</dict>

</dict>

Can I write a file directly to iCloud Drive, or I have to write to a local file first and then move to iCloud Drive?

my code is as below:


let localDocsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last!

let localFile = localDocsURL.appendingPathComponent("\(file)")

do {

try line.write(to: localFile, atomically: true, encoding: .utf8)

}

catch { os_log("Failed to export...", log: OSLog.default, type: .error) }

if let iCloudDocsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents") {

let iCloudFile = iCloudDocsURL.appendingPathComponent("\(file)")

if !FileManager.default.fileExists(atPath: iCloudDocsURL.path, isDirectory: nil) {

try? FileManager.default.createDirectory(at: iCloudDocsURL, withIntermediateDirectories: true, attributes: nil)

}

do {

try FileManager.default.copyItem(at: localFile, to: iCloudFile)

}

catch { os_log("Failed to move to iCloud...", log: OSLog.default, type: .error) }

}

The file exists on iCloud->Manage Storage->AppName on my iPhone,

but it can't be shown on my MacBook Pro or iCloud.com

I have the same problem. Did you figure it out?
How to write a file to iCloud Drive Documents folder?
 
 
Q