选择 iCloud 文件并发送

选择 iCloud 文件并发送 RCFileMessage 类型消息。
发布时间: 2019-07-18 14:46

回答:

iCloud 选择文件功能

1: 在扩展功能板的点击回调代理方法中判断如果tag是PLUGIN_BOARD_ITEM_FILE_TAG 则拦截,会话页面重写如下方法:

- (void)pluginBoardView:(RCPluginBoardView *)pluginBoardView 
clickedItemWithTag:(NSInteger)tag;

2:调用如下方法唤起 iCloud 选择界面:

// 根据需求添加具体文件格式
NSArray *documentTypes = @[@"public.content",
                               @"public.text",
                               @"public.source-code",
                               @"public.image",
                               @"public.audiovisual-content",
                               @"com.adobe.pdf",
                               @"com.apple.keynote.key",
                               @"com.microsoft.word.doc",
                               @"com.microsoft.excel.xls",
                               @"com.microsoft.powerpoint.ppt"];
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeImport];
    documentPicker.delegate = self;
    documentPicker.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:documentPicker animated:YES completion:nil];

3:选择完文件后调用如下方法发出消息:

#pragma mark - UIDocumentPickerDelegate
-(void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
    NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
    NSError *error = nil;
    [coordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
        NSData *data = [NSData dataWithContentsOfURL:newURL];
        if (data.length <= 0) {
            UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:RCEStringFor(@"not_allow_empty") message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:RCEStringFor(@"confirm_btn_title") style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

            }];
            [alertCtrl addAction:confirmAction];
            [self presentViewController:alertCtrl animated:YES completion:nil];
            return;
        }
        RCFileMessage *fileMsg = [RCFileMessage messageWithFile:[newURL path]];
        [self sendMessage:fileMsg pushContent:nil];
        [controller dismissViewControllerAnimated:YES completion:nil];
    }];
    if (error) {
        NSLog(@"ERROR: read data with "DocumentAtURL" failed");
    }
}

注意:App 需要在 Capabilities 中开启 iCloud Documents 功能。