-->

Accessing iCloud Drive files from app

2019-06-24 06:48发布

问题:

I want to have iCloud support in my app where I can store and get PDF files. My question is "How to access iCloud drive file without using UIDocumentPickerViewController". Actually I want to have custom files list in my app.

I tried this code

- (void) getAllFilesIniCloud
{
    queryData = [[NSMetadataQuery alloc] init];
    [queryData setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope]];
    NSPredicate *pred = [NSPredicate predicateWithFormat: @"%K like '*.'", NSMetadataItemFSNameKey];
    [queryData setPredicate:pred];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(queryDidFinishGathering:)
                                                 name:NSMetadataQueryDidFinishGatheringNotification
                                               object:queryData];
    [queryData startQuery];
}

- (void)queryDidFinishGathering:(NSNotification *)notification
{
    NSMetadataQuery *pQuery = [notification object];
    [pQuery disableUpdates];
    [pQuery stopQuery];
    [pFilesArray removeAllObjects];
    for (NSMetadataItem *item in [query results])
    {
        NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
        NSString * name = url.lastPathComponent;

        if ([[name pathExtension] isEqualToString:@"pdf"] || [[name pathExtension] isEqualToString:@"PDF"])
        {
            NSString *fileName = [url.absoluteString lastPathComponent];

            if (url && ![pFilesArray containsObject:fileName])
            {
                [pFilesArray addObject:fileName];
            }

        }
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:pQuery];

    queryData = nil;
    [pTableView reloadData];
}

and also tried the scope

NSMetadataQueryUbiquitousDataScope
NSMetadataQueryUbiquitousDocumentsScope
NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope

I know this code working fine for app directory in iCloud. But if user store file in iCloud in Mac or through web then my app should also enable to access this file.

回答1:

I had the same problem , please go through this thread to clarify on iCloud https://forums.xamarin.com/discussion/comment/283726#Comment_283726