UIDocumentInteractionController 实现本地文件预览和原生分享

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackgroundColor;
    self.navigationItem.title = @"文件预览";
    UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:self.url]];
    documentController.delegate = self;
        //直接显示预览
    [documentController presentPreviewAnimated:YES];
        //显示包含预览的菜单项
    [documentController presentOptionsMenuFromRect:CGRectZero inView:self.navigationController.view animated:YES];
}
#pragma mark -- UIDocumentInteractionControllerDelegate
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller
{
    //修改QLPreviewController导航栏颜色
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:[QLPreviewController class], nil];
    [navBar setBackgroundImage:[UIImage createImageWithColor:kDefaultColor size:CGSizeMake(kScreenWidth, kNavHeight)] forBarMetrics:UIBarMetricsDefault];

    return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
    return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
    return self.view.frame;
}
//点击预览窗口的“Done”(完成)按钮时调用

-(void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController*)controller {
    [self.navigationController popViewControllerAnimated:NO];
}
// 弹框消失的时候走的方法

-(void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController*)controller{
    [self.navigationController popViewControllerAnimated:NO];
}
// 文件分享面板弹出的时候调用

- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController*)controller{
}

// 当选择一个文件分享App的时候调用

- (void)documentInteractionController:(UIDocumentInteractionController*)controller willBeginSendingToApplication:(nullable NSString*)application{
    [self.navigationController popViewControllerAnimated:NO];
}
21588914580_.pic_hd.jpg

你可能感兴趣的:(UIDocumentInteractionController 实现本地文件预览和原生分享)