UIDocumentInteractionController使用

DEMO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#import "ViewController.h"

@interface ViewController ()<UIDocumentInteractionControllerDelegate>

@property (nonatomic, strong) UIDocumentInteractionController *document;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

NSURL *url = [[NSBundle mainBundle] URLForResource:@"pdffile" withExtension:@"pdf"];
// NSString *docu = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// NSString *filePath = [docu stringByAppendingPathComponent:@"004.png"];
// NSURL *url = [NSURL fileURLWithPath:filePath];


self.document = [UIDocumentInteractionController interactionControllerWithURL:url];
self.document.delegate = self;

// 不展示可选操作
// [self.document presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];


// 展示可选操作
// 可结合代理方法documentInteractionControllerViewControllerForPreview:显示预览
[self.document presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];

}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
/**
* 文件分享面板退出时调用
*/
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
NSLog(@"dismiss");
}

/**
* 文件分享面板弹出的时候调用
*/
- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller {
NSLog(@"WillPresentOpenInMenu");

}

/**
* 当选择一个文件分享App的时候调用
*/
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application {
NSLog(@"begin send : %@", application);
}
@end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

/*
UIDocumentInteractionController是iOS 很早就出来的一个功能。但由于平时很少用到,压根就没有听说过它。而我们忽略的却是一个功能强大的”文档阅读器”。
UIDocumentInteractionController主要有两个功能,一个是文件预览,另一个就是调用iPhoneh里第三方相关的app打开文档(注意这里不是根据url scheme 进行识别,而是苹果的自动识别)
*/


import UIKit

class ViewController: UIViewController,UIDocumentInteractionControllerDelegate {


var documentController:UIDocumentInteractionController!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let docUrl = Bundle.main.url(forResource: "test1", withExtension: "pdf")

documentController = UIDocumentInteractionController(url:docUrl!)
documentController.delegate = self;

}

@IBAction func preViewDocument(_ sender: AnyObject) {

//当前APP打开 需实现协议方法才可以完成预览功能
documentController.presentPreview(animated: true)

}

@IBAction func openDocument(_ sender: AnyObject) {

//第三方打开 手机中安装有可以打开此格式的软件都可以打开
documentController.presentOpenInMenu(from: (sender as! UIButton).frame, in: self.view, animated: true)

}

public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController
{
return self
}


}