- (BOOL)downloadFileIfNotAvailable:(NSURL*)file {
NSNumber* isIniCloud = nil;
if ([file getResourceValue:&isIniCloud forKey:NSURLIsUbiquitousItemKey error:nil]) {
// If the item is in iCloud, see if it is downloaded.
if ([isIniCloud boolValue]) {
NSNumber* isDownloaded = nil;
if ([file getResourceValue:&isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:nil]) {
if ([isDownloaded boolValue])
return YES;
// Download the file.
NSFileManager* fm = [NSFileManager defaultManager];
NSError *downloadError = nil;
[fm startDownloadingUbiquitousItemAtURL:file error:&downloadError];
if (downloadError) {
NSLog(@"Error occurred starting download: %@", downloadError);
}
return NO;
}
}
}
// Return YES as long as an explicit download was not started.
return YES;
}
startDownloadingUbiquitousItemAtURL :error:
Starts downloading (if necessary) the specified item to the local system.
Parameters
- url
- Specify the URL for the file or directory in the cloud that you want to download.
- errorOut
- On input, a pointer to variable for an
NSError
object. If an error occurs, this pointer is set to anNSError
object containing information about the error. You may specifynil
for this parameter if you do not want the error information.
Return Value of [NSFileManager startDownloadingUbiquitousItemAtURL]
YES
if the download started successfully or was not necessary, otherwise NO
. If NO
is returned and errorOut is not nil
, an NSError
object describing the error is returned in that parameter.Discussion of [NSFileManager startDownloadingUbiquitousItemAtURL]
If a cloud-based file or directory has not been downloaded yet, calling this method starts the download process. If the item exists locally, calling this method synchronizes the local copy with the version in the cloud.
For a given URL, you can determine if a file is downloaded by getting the value of the
NSMetadataUbiquitousItemIsDownloadedKey
key. You can also use related keys to determine the current progress in downloading the file.