Try this for download iCloudFile:
url.startAccessingSecurityScopedResource()
if !FileManager.default.fileExists(atPath: url.path), FileManager.default.isUbiquitousItem(at: url) {
try FileManager.default.startDownloadingUbiquitousItem(at: url)
startReadingStatus(for: url) { isSuccess in
if isSuccess {
do {
try FileManager.default.copyItem(at: url, to: LOCAL_URL)
} catch {
print(error)
}
}
url.stopAccessingSecurityScopedResource()
}
return true
}
internal func startReadingStatus(for url: URL, completion: ((Bool) -> Void)? = nil) {
DispatchQueue.main.async {
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { [weak self] (timer) in
guard let status = self?.downloadStatus(for: url)
else {
completion?(false)
return
}
if status == URLUbiquitousItemDownloadingStatus.current {
timer.invalidate()
completion?(true)
}
})
}
}
internal func downloadStatus(for url: URL) -> URLUbiquitousItemDownloadingStatus? {
do {
let attributes = try url.resourceValues(forKeys: [URLResourceKey.ubiquitousItemDownloadingStatusKey])
guard let status: URLUbiquitousItemDownloadingStatus = attributes.allValues[URLResourceKey.ubiquitousItemDownloadingStatusKey] as? URLUbiquitousItemDownloadingStatus
else {
return nil
}
return status
} catch {
return nil
}
}