1

我使用 raywenderlich https://www.raywenderlich.com/6015/beginning-icloud-in-ios-5-tutorial-part-1将 iCloud 集成到 iOS 应用程序中

但我无法将 iCloud 中的所有文件显示到我们的 iOS 应用程序,还需要特定类型的文件,如 pdf、doc 和 docx

任何人都可以建议我。

4

2 回答 2

2

按照以下步骤将 iCloud 集成到 iOS 应用程序中并检索文件。1. 从您的开发者帐户启用 iCloud。2. 在开发者账户创建 iCloud 容器授权。3. 然后只需在您想要集成 iCloud 集成的地方使用以下代码。

首先导入#import并添加iCloudDelegate委托,然后设置委托:

                // Setup iCloud
            [[iCloud sharedCloud] setDelegate:self];
            [[iCloud sharedCloud] setVerboseLogging:YES];
            [[iCloud sharedCloud] setupiCloudDocumentSyncWithUbiquityContainer:nil];

            [self showiCloudFiles];

然后执行下面的方法showiCloudFiles

-(void) showiCloudFiles{

BOOL cloudAvailable = [[iCloud sharedCloud] checkCloudAvailability];
if (cloudAvailable && [[NSUserDefaults standardUserDefaults] boolForKey:@"userCloudPref"] == YES) {
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.content"]
                                                                                                            inMode:UIDocumentPickerModeImport];
    documentPicker.delegate = self;
    documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:documentPicker animated:YES completion:nil];

}
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"userCloudPref"] == NO) {

    UIAlertController * alert = SIMPLE_ALERT_VIEW(@"iCloud Disabled", @"You have disabled iCloud for this app. Would you like to turn it on again?");
    UIAlertAction* cancelButton = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){}];[alert addAction:cancelButton];
    UIAlertAction* deleteButton = [UIAlertAction actionWithTitle:@"Turn On iCloud"
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action){
                                                             [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"userCloudPref"];
                                                             [[NSUserDefaults standardUserDefaults] synchronize];

                                                             BOOL cloudAvailable = [[iCloud sharedCloud] checkCloudAvailability];
                                                             if (cloudAvailable && [[NSUserDefaults standardUserDefaults] boolForKey:@"userCloudPref"] == YES) {
                                                                 UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.content"]
                                                                                                                                                                         inMode:UIDocumentPickerModeImport];
                                                                 documentPicker.delegate = self;
                                                                 documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
                                                                 [self presentViewController:documentPicker animated:YES completion:nil];

                                                             }
                                                         }];
    [alert addAction:deleteButton];
    [self presentViewController:alert animated:YES completion:nil];

} else {

    UIAlertController * alert = SIMPLE_ALERT_VIEW(@"Setup iCloud", @"iCloud is not available. Sign into an iCloud account on this device and check that this app has valid entitlements.");
    UIAlertAction* cancelButton = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){}];[alert addAction:cancelButton];                                                                
                                                         }];
    [self presentViewController:alert animated:YES completion:nil];

}

}

之后下载文件使用 UIDocumentPickerDelegate 方法:

 #pragma mark - UIDocumentPickerDelegate
-(void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url{
if (controller.documentPickerMode == UIDocumentPickerModeImport) {
    //NSLog(@"%@",url);


    [url startAccessingSecurityScopedResource];
    NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] init];
    NSError *error;
    __block NSData *fileData;

    [coordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingForUploading error:&error byAccessor:^(NSURL *newURL) {
// File name for use in writing the file out later
NSString *fileName = [newURL lastPathComponent]; NSString *fileExtension = [newURL pathExtension]; if([fileExtension isEqualToString:@"zip"]) {if([[[newURL URLByDeletingPathExtension] pathExtension] isEqualToString:@"pages"] ||
      [[[newURL URLByDeletingPathExtension] pathExtension] isEqualToString:@"numbers"] ||
      [[[newURL URLByDeletingPathExtension] pathExtension] isEqualToString:@"key"] ) {
       // Remove .zip if it is an iWork file
       fileExtension = [[newURL URLByDeletingPathExtension] pathExtension];
       fileName = [[newURL URLByDeletingPathExtension] lastPathComponent];
}
} 
NSError *fileConversionError;fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingUncached error:&fileConversionError];
        // Do further code using  fileData
        }
    }];
[url stopAccessingSecurityScopedResource];
    }
}

对于UIDocumentPicker,请访问此链接iOS-8-UIDocumentPicker

于 2016-06-07T07:33:27.087 回答
1

遵循本指南

https://www.raywenderlich.com/12779/icloud-and-uidocument-beyond-the-basics-part-1

下载示例代码

https://github.com/rwenderlich/PhotoKeeper

检查 iCloud 是否可用

- (void)initializeiCloudAccessWithCompletion:(void (^)(BOOL available)) completion {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        _iCloudRoot = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
        if (_iCloudRoot != nil) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"iCloud available at: %@", _iCloudRoot);
                completion(TRUE);
            });            
        }            
        else {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"iCloud not available");
                completion(FALSE);
            });
        }
    });
}

查询 pdf、doc 和 docx 等苍蝇类型

- (NSMetadataQuery *)documentQuery {

    NSMetadataQuery * query = [[NSMetadataQuery alloc] init];
    if (query) {

        // Search documents subdir only
        [query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];

        // Add a predicate for finding the documents
        NSString * filePattern = [NSString stringWithFormat:@"*.%@", PTK_EXTENSION];
        [query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@",
                             NSMetadataItemFSNameKey, filePattern]];        

    }
    return query;

}
于 2016-05-08T02:02:50.023 回答