I am unable to download file from iCloud Documents from my swift project. I was able to successfully upload the file to iCloud Documents but now when I try to download same file at the uploaded path. It is giving me error that I don't have permission to view the file.
Please find exact error below.
[ERROR] couldn't fetch remote operation IDs: NSError: Cocoa 257 "The file couldn’t be opened because you don’t have permission to view it." "Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)""
I did reproduce the following error when playing your project:
[ERROR] couldn't fetch remote operation IDs: NSError: Cocoa 257 "The file couldn’t be opened because you don’t have permission to view it." "Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)""
I believe this is a log noice that you can safely ignore, as mentioned in On Log Noise.
Other than that, I see several issues in your project.
First, your following code uses a wrong search scope:
query.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope]
Since your data isn't in the Documents
folder, the search scopes should be NSMetadataQueryUbiquitousDataScope
:
query.searchScopes = [NSMetadataQueryUbiquitousDataScope]
Secondly, your following code doesn't retain the metadata query (NSMetadataQuery
):
@IBAction func didTapOnButtonRestore(_ sender: Any) { ... iCloudFileMonitor().startMonitoring(for: "Sample.txt") }
iCloudFileMonitor()
will be released after your app exits didTapOnButtonRestore
, and so its member variable metadataQuery
will be released as well. To fix that, consider creating a member variable to hold the monitor:
class ViewController: UIViewController {
private let monitor = iCloudFileMonitor()
...
@IBAction func didTapOnButtonRestore(_ sender: Any) {
...
//iCloudFileMonitor().startMonitoring(for: "Sample.txt")
monitor.startMonitoring(for: "Sample.txt")
}
}
With that, the metadata query should monitor the data scope of your ubiquity container, and you can do your data backup or restoration based on the query result.
Fore more details about how to use NSMetadataQuery
, see the following sample code:
Best,
——
Ziqiao Chen
Worldwide Developer Relations.