gpt4 book ai didi

objective-c - NSMetadataQuery 找不到 iCloud 文件

转载 作者:太空狗 更新时间:2023-10-30 03:42:25 25 4
gpt4 key购买 nike

我按照 Apple Docs NSMetadataQuery 中的描述使用它来搜索我的 iCloud 文件。我只有一个文件,而且我知道它的名字。我的问题是有时这个文件不存在(我猜是因为它还没有被下载)并且 NSMetadataQuery 无法找到它。

曾经尝试使用 NSFileManager startDownloadingUbiquitousItemAtURL:error: 强制下载,但它返回了一个错误。 (阅读编辑)

我的解决方案是,我第一次创建文件,然后我猜它存在,然后用 UIDocument 打开它。但它可能不存在,或者它可能是用户第一次打开应用程序。我不能确定这些事情。我的第一个问题是:如果 UIDocument 打开文件,则意味着它在某处找到了文件。如果文件不存在,它如何使用该文件?

然后,第二个问题:如果我的应用程序必须管理多个文件或名称未知的文件。如果 NSMetadataQuery 不起作用,我如何找到它们。

编辑:如果应该使用 startDownloadingUbiquitousItemAtURL 开始下载文件,我如何知道文件何时完成下载(可能带有通知)?但是,更重要的是:如果总是说(删除原始名称),我该如何下载文件?

   Error Domain=NSPOSIXErrorDomain Code=2 "The operation couldn’t be completed.
No such file or directory" UserInfo=0x166cb0 {
NSDescription=Unable to get real path for Path
'/private/var/mobile/Library/Mobile Documents/teamid~com~team~app/Documents/file.extension'
}

最佳答案

我建议对 iCloud 文件执行以下加载例程。它包括 4 个步骤:

  1. 首先,测试是否可以访问 iCloud
  2. 然后查找您的文件(查找您指定的特定文件或查找具有特定扩展名(如 *.txt)的所有文件,或者如果您真的不知道您要查找的文件扩展名,例如 NSPredicate *pred = [NSPredicate predicateWithFormat:@"NOT %K.pathExtension = '.'", NSMetadataItemFSNameKey]; 将返回所有具有扩展名的文件,如 jpg、txt、dat 等)<
  3. 然后检查查询是否完成,并且
  4. 最后尝试加载文件。如果该文件不存在,请创建它。如果确实存在,则加载它。

下面是这四个步骤的示例代码:

    - (void)loadData:(NSMetadataQuery *)query {

// (4) iCloud: the heart of the load mechanism: if texts was found, open it and put it into _document; if not create it an then put it into _document

if ([query resultCount] == 1) {
// found the file in iCloud
NSMetadataItem *item = [query resultAtIndex:0];
NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];

MyTextDocument *doc = [[MyTextDocument alloc] initWithFileURL:url];
//_document = doc;
doc.delegate = self.viewController;
self.viewController.document = doc;

[doc openWithCompletionHandler:^(BOOL success) {
if (success) {
NSLog(@"AppDelegate: existing document opened from iCloud");
} else {
NSLog(@"AppDelegate: existing document failed to open from iCloud");
}
}];
} else {
// Nothing in iCloud: create a container for file and give it URL
NSLog(@"AppDelegate: ocument not found in iCloud.");

NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:@"text.txt"];

MyTextDocument *doc = [[MyTextDocument alloc] initWithFileURL:ubiquitousPackage];
//_document = doc;
doc.delegate = self.viewController;
self.viewController.document = doc;

[doc saveToURL:[doc fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
NSLog(@"AppDelegate: new document save to iCloud");
[doc openWithCompletionHandler:^(BOOL success) {
NSLog(@"AppDelegate: new document opened from iCloud");
}];
}];
}
}

- (void)queryDidFinishGathering:(NSNotification *)notification {

// (3) if Query is finished, this will send the result (i.e. either it found our text.dat or it didn't) to the next function

NSMetadataQuery *query = [notification object];
[query disableUpdates];
[query stopQuery];

[self loadData:query];

[[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];
_query = nil; // we're done with it
}

-(void)loadDocument {

// (2) iCloud query: Looks if there exists a file called text.txt in the cloud

NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
_query = query;
//SCOPE
[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
//PREDICATE
NSPredicate *pred = [NSPredicate predicateWithFormat: @"%K == %@", NSMetadataItemFSNameKey, @"text.txt"];
[query setPredicate:pred];
//FINISHED?
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query];
[query startQuery];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"AppDelegate: app did finish launching");
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
} else {
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
}

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

// (1) iCloud: init

NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (ubiq) {
NSLog(@"AppDelegate: iCloud access!");
[self loadDocument];
} else {
NSLog(@"AppDelegate: No iCloud access (either you are using simulator or, if you are on your phone, you should check settings");
}


return YES;
}

关于objective-c - NSMetadataQuery 找不到 iCloud 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7950804/

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