<code
class
=
"language-iOS hljs objectivec"
>
1
、首先要遵循UIDocumentInteractionControllerDelegate
02.
03.
2
、其次是创建一个UIDocumentInteractionController对象
04.
@property
(nonatomic,retain)UIDocumentInteractionController *docController;
05.
06.
3
、在方法中进行UIDocumentInteractionController对象的初始化
07.
08.
- (
void
)open{
09.
NSString *fileName = [self.listDicobjectForKey:@
"fileName"
];
10.
11.
NSString* path = [NSHomeDirectory()stringByAppendingPathComponent:
12.
13.
_docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];
//为该对象初始化一个加载路径
14.
15.
_docController.delegate =self;
//设置代理
16.
17.
//直接显示预览
18.
// [_docController presentPreviewAnimated:YES];
19.
20.
CGRect navRect =self.navigationController.navigationBar.frame;
21.
navRect.size =CGSizeMake(
1500
.0f,
40
.0f);
22.
23.
//显示包含预览的菜单项
24.
[_docController presentOptionsMenuFromRect:navRectinView:self.viewanimated:YES];
25.
26.
//显示不包含预览菜单项
27.
//[docController presentOpenInMenuFromRect:navRect inView:self.view animated:YES];
28.
}
29.
4
、代理方法
30.
31.
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
32.
{
33.
return
self;
34.
}
35.
36.
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
37.
{
38.
return
self.view;
39.
}
40.
41.
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
42.
{
43.
return
self.view.frame;
44.
}</code>
使用时的注意事项
二、UIDocumentInterRactionController使用时的注意事项
1、UIDocumentInterRactionController定义的时候一定要是retain类型的,因为必须要对该对象进行持有;
2、当选择显示包含预览的菜单项
[_docController presentOptionsMenuFromRect:navRectinView:self.viewanimated:YES];时应该注意一点,该方法会触发该类的内置打印输出,会将日志信息打印出来,从而导致App崩溃严重的可能会导致手机死机,现在还未找到解决方法。类似输出结果如下(此处省略该输出的后面部分内容,因为太多了):