美文网首页iOS新手学习
iOS UIDocumentInteractionControl

iOS UIDocumentInteractionControl

作者: 清风_____ | 来源:发表于2022-03-29 10:17 被阅读0次

    使用系统控件UIDocumentInteractionController对多种格式的文件进行预览,或分享等的操作。

    // 获取文件路径

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"read" withExtension:@"txt"];
    // 或
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"performanceOptimization" ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:filePath]; // 注意:使用[NSURL URLWithString:filePath]无效
    

    // 实例化UIDocumentInteractionController

    UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
    documentController.delegate = self;
    

    // 文件操作

    // 1 预览
    [documentController presentPreviewAnimated:YES]; // 预览文件
    
    // 2 分享等
    [documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES]; // 菜单操作
    

    // 实现代理,添加协议 UIDocumentInteractionControllerDelegate

    - (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(nullable SEL)action
    {
        // 响应方法
        NSLog(@"12 %s", __func__);
        return YES;
    }
     
    - (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller performAction:(nullable SEL)action
    {
        //
        NSLog(@"13 %s", __func__);
        return YES;
    }
     
    - (void)documentInteractionControllerWillPresentOptionsMenu:(UIDocumentInteractionController *)controller
    {
        // 页面显示后响应
        NSLog(@"9 %s", __func__);
    }
     
    - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
    {
        // 取消时响应
        NSLog(@"10 %s", __func__);
    }
     
    - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
    {
        NSLog(@"1 %s", __func__);
        return self;
    }
     
    - (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
    {
        NSLog(@"2 %s", __func__);
        return self.view;
    }
     
    - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
    {
        NSLog(@"3 %s", __func__);
        return self.view.frame;
    }
     
    // 文件分享面板退出时调用
    - (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller
    {
        NSLog(@"4 %s", __func__);
        NSLog(@"dismiss");
    }
     
    // 文件分享面板弹出的时候调用
    - (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller
    {
        NSLog(@"5 %s", __func__);
        NSLog(@"WillPresentOpenInMenu");
    }
     
    // 当选择一个文件分享App的时候调用
    - (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application
    {
        NSLog(@"6 %s", __func__);
        NSLog(@"begin send : %@", application);
    }
     
    // Preview presented/dismissed on document.  Use to set up any HI underneath.
    - (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller
    {
        NSLog(@"7 %s", __func__);
    }
    - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
    {
        // 完成时响应
        NSLog(@"8 %s", __func__);
    }
     
    - (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(nullable NSString *)application
    {
        NSLog(@"11 %s", __func__);
    }
    
    01.png 02.png 03.png

    https://blog.csdn.net/potato512/article/details/80177009
    http://blog.sina.com.cn/s/blog_bc3421b10101a357.html
    http://www.360doc.com/content/16/0128/19/8772388_531302460.shtml
    https://www.jianshu.com/p/c9484a4e9fc8

    相关文章

      网友评论

        本文标题:iOS UIDocumentInteractionControl

        本文链接:https://www.haomeiwen.com/subject/mrjvjrtx.html