iOS 从 iCloud Drive 下载文件
iOS download file from iCloud Drive
在我 iOS 的应用程序中,我将一些文件存储到 iCloud Drive 文件夹以进行备份。现在我想检索该文件,但我不知道这样做的正确方法是什么。我的意思是如果 iCloud Drive 有任何特定的方法,或者我可以从 url.
中获取它
我是这样把文件存到iCloud的():
- (void) storeToIcloud
{
// Let's get the root directory for storing the file on iCloud Drive
[self rootDirectoryForICloud:^(NSURL *ubiquityURL) {
if (ubiquityURL) {
// We also need the 'local' URL to the file we want to store
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"myFile.xml"]; //Add the file name
NSURL *localURL = [NSURL fileURLWithPath:filePath];
// Now, append the local filename to the ubiquityURL
ubiquityURL = [ubiquityURL URLByAppendingPathComponent:localURL.lastPathComponent];
// And finish up the 'store' action
NSError *error;
if (![[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:localURL destinationURL:ubiquityURL error:&error]) {
NSLog(@"Error occurred: %@", error);
}
}
else {
NSLog(@"Could not retrieve a ubiquityURL");
}
}];
}
- (void)rootDirectoryForICloud:(void (^)(NSURL *))completionHandler {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *rootDirectory = [[[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]URLByAppendingPathComponent:@"Documents"];
if (rootDirectory) {
if (![[NSFileManager defaultManager] fileExistsAtPath:rootDirectory.path isDirectory:nil]) {
NSLog(@"Create directory");
[[NSFileManager defaultManager] createDirectoryAtURL:rootDirectory withIntermediateDirectories:YES attributes:nil error:nil];
}
}
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(rootDirectory);
});
});
}
- (NSURL *)localPathForResource:(NSString *)resource ofType:(NSString *)type {
NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *resourcePath = [[documentsDirectory stringByAppendingPathComponent:resource] stringByAppendingPathExtension:type];
return [NSURL fileURLWithPath:resourcePath];
}
如何从 iCloud 驱动器下载文件?
提前致谢。
您应该使用 NSMetadataQuery 从 icloud 获取项目,然后当您将书签挂接到文件时使用 NSFileCoordination 读取它,可以使用相同的方法从云或您的应用程序沙箱中读取...此处详细解释了所有内容:Building Document Based Apps,您甚至可以遵循一些示例代码。希望这对您有所帮助!
在我 iOS 的应用程序中,我将一些文件存储到 iCloud Drive 文件夹以进行备份。现在我想检索该文件,但我不知道这样做的正确方法是什么。我的意思是如果 iCloud Drive 有任何特定的方法,或者我可以从 url.
中获取它我是这样把文件存到iCloud的():
- (void) storeToIcloud
{
// Let's get the root directory for storing the file on iCloud Drive
[self rootDirectoryForICloud:^(NSURL *ubiquityURL) {
if (ubiquityURL) {
// We also need the 'local' URL to the file we want to store
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"myFile.xml"]; //Add the file name
NSURL *localURL = [NSURL fileURLWithPath:filePath];
// Now, append the local filename to the ubiquityURL
ubiquityURL = [ubiquityURL URLByAppendingPathComponent:localURL.lastPathComponent];
// And finish up the 'store' action
NSError *error;
if (![[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:localURL destinationURL:ubiquityURL error:&error]) {
NSLog(@"Error occurred: %@", error);
}
}
else {
NSLog(@"Could not retrieve a ubiquityURL");
}
}];
}
- (void)rootDirectoryForICloud:(void (^)(NSURL *))completionHandler {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *rootDirectory = [[[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]URLByAppendingPathComponent:@"Documents"];
if (rootDirectory) {
if (![[NSFileManager defaultManager] fileExistsAtPath:rootDirectory.path isDirectory:nil]) {
NSLog(@"Create directory");
[[NSFileManager defaultManager] createDirectoryAtURL:rootDirectory withIntermediateDirectories:YES attributes:nil error:nil];
}
}
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(rootDirectory);
});
});
}
- (NSURL *)localPathForResource:(NSString *)resource ofType:(NSString *)type {
NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *resourcePath = [[documentsDirectory stringByAppendingPathComponent:resource] stringByAppendingPathExtension:type];
return [NSURL fileURLWithPath:resourcePath];
}
如何从 iCloud 驱动器下载文件?
提前致谢。
您应该使用 NSMetadataQuery 从 icloud 获取项目,然后当您将书签挂接到文件时使用 NSFileCoordination 读取它,可以使用相同的方法从云或您的应用程序沙箱中读取...此处详细解释了所有内容:Building Document Based Apps,您甚至可以遵循一些示例代码。希望这对您有所帮助!