gpt4 book ai didi

iphone - 将数据上传至 iCloud

转载 作者:行者123 更新时间:2023-11-29 04:09:35 24 4
gpt4 key购买 nike

我正在尝试使用以下代码将文档上传到 iCloud

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.png"];
NSURL* sourceURL = [[NSURL alloc]initWithString:path];

NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:[sourceURL path]]) {
NSLog(@"File found!");
}
else{
NSLog(@"File not found!");
}



NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (ubiq) {
NSLog(@"iCloud access at %@", ubiq);
NSError* error = nil;
NSURL *destinationURL = [ubiq URLByAppendingPathComponent:@"Documents/test.png"];
[[NSFileManager defaultManager] setUbiquitous:YES
itemAtURL:sourceURL
destinationURL:destinationURL
error:&error];
if(error != nil){
NSLog(@"Error while uploading the file: %@", error);
}
else{
NSLog(@"File ulpoaded successfully");
}
else {
NSLog(@"No iCloud access");
}

我尝试上传的文件存在(打印“找到文件”),但将其上传到 iCloud 时会生成以下错误

Error while uploading the file: Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x1dd6bda0 {NSURL=/var/mobile/Applications/9F496F51-9DCA-4379-A62E-FFF7D7AB7385/VerticalSwipeArticles.app/test.png, NSUnderlyingError=0x1dd6c120 "The operation couldn’t be completed. Operation not permitted"}

谢谢

最佳答案

使用 GCD 并从不同的线程调用 iCloud 代码(特别是 NSURL *ubiq =... 行)。在某些情况下,在主线程上调用它可能会阻止应用程序。

你构建 iCloud 路径的方式对我来说看起来很奇怪。试试这个:

NSURL *destinationURL = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:@"test.png"];

关于iphone - 将数据上传至 iCloud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14628861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com