美文网首页
DocumentInteractionViewControlle

DocumentInteractionViewControlle

作者: 柏斯特湾 | 来源:发表于2017-08-18 15:50 被阅读70次

在程序中,有时候会从服务器下载一些文件来查看,比如word pdf文件,但是我们自己的应用不具备查看此类文件的功能。苹果给我们提供了DocumentInteractionViewController类,下面我们介绍这个类的如何使用,然后贴上自己的小demo供大家参考:

DocumentInteractionViewController demo

第一步:

创建DocumentInteractionViewController的实例,这其中就包含了你要打开那个文件的url,然后设定代理,实现代理方法,一会儿介绍代理方法的含义:

NSString *path = [[NSBundle mainBundle] pathForResource:@" 搭建React Native生态  魏晓军  2016-06-24 " ofType:@"pdf"];

NSURL * url = [NSURL fileURLWithPath:path];

self.documentVC = [UIDocumentInteractionController interactionControllerWithURL:url];

self.documentVC.delegate = self;

第二步:介绍几个此类的方法

1. 快速预览,返回bool值,表示可不可以预览

BOOL b = [self.documentVC presentPreviewAnimated:YES];

2. 弹出选项菜单由用户自己选择copy,print,quick look操作

BOOL b = [self.documentVC presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];

3. 通过item打开选项,这里需要首先创建item

UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithTitle:@"打开文件" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];

self.navigationItem.leftBarButtonItem = item;

然后在item的点击事件中,调用presentOptionsMenuFromBarButtonItem方法

- (void)itemAction:(UIBarButtonItem *)sender

{

NSLog(@"%@",NSStringFromSelector(_cmd));

[self.documentVC presentOptionsMenuFromBarButtonItem:sender animated:YES];

}

4. 通过其他应用程序打开文件,如果没有应用程序可以打开,返回NO

BOOL result = [self.documentVC presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];

if (!result) {

NSLog(@"没有可以打开此文件的应用");

}

5. 通过item让其他应用打开文件,如果没有应用程序可以打开,返回NO

类似第3步,先创建item,

UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(itemActionOpenInMenu:)];

self.navigationItem.rightBarButtonItem = item;

然后在item的点击事件里,执行第4步。

- (void)itemActionOpenInMenu:(UIBarButtonItem *)sender

{

NSLog(@"%@",NSStringFromSelector(_cmd));

BOOL result = [self.documentVC presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];

if (!result) {

NSLog(@"没有可以打开此文件的应用");

}

}

第三步:介绍代理方法

#pragma mark 代理方法

//为快速预览指定控制器

- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller

{

NSLog(@"%@",NSStringFromSelector(_cmd));

return self;

}

//为快速预览指定View

- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller

{

NSLog(@"%@",NSStringFromSelector(_cmd));

return self.view;

}

//为快速预览指定显示范围

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller

{

NSLog(@"%@",NSStringFromSelector(_cmd));

//    return self.view.frame;

return CGRectMake(0, 0, self.view.frame.size.width, 300);

}


相关文章

  • DocumentInteractionViewControlle

    在程序中,有时候会从服务器下载一些文件来查看,比如word pdf文件,但是我们自己的应用不具备查看此类文件的功能...

网友评论

      本文标题:DocumentInteractionViewControlle

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