iOS UIDocumentInteractionController

UIDocumentInteractionController 是 OC 语言的一个类,但是他并不是一个 controller,而是一个继承自 NSObject 类。

1.作用

(1)预览类似 pdfdocppt等类型文件的类。
(2)可以将用户接收到的文件分享到用户手机上的其他 App 中。

2.使用
#import "ViewController.h"
@interface ViewController ()<UIDocumentInteractionControllerDelegate>

@property (nonatomic,strong)UIDocumentInteractionController * document;

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    //1.指定要分享的链接
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"文件" ofType:@"pdf"];
    NSLog(@"data path: %@", filePath);
    self.document = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
    //2.设置分享代理
    self.document.delegate = self;
    //3.哪类文件支持第三方打开,这里不证明就代表所有文件!
    //    _document.UTI = @"com.microsoft.word.doc";
    //4.判断手机中有没有应用可以打开该文件并打开分享界面
    // 用户预览文件,如图1所示
    BOOL canOpen = [self.document presentPreviewAnimated:YES];

    // 用户不预览文件直接分享,如图2所示
//    BOOL canOpen = [self.document presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];
    
    if (!canOpen) {
        NSLog(@"预览失败");
    }
    
}
-(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__);
}

@end
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容