Access iCloud Drive files from the app

I want to have iCloud support in my application, where I can store and receive PDF files. My question is: "How to access the iCloud drive file without using UIDocumentPickerViewController." Actually, I want to have a list of custom files in my application.

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 area

NSMetadataQueryUbiquitousDataScope
NSMetadataQueryUbiquitousDocumentsScope
NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope

I know this code works fine for the iCloud app directory. But if the user stores the file in iCloud on a Mac or over the Internet, my application should also enable access to this file.

+4
source share

Source: https://habr.com/ru/post/1610952/


All Articles