文章主要写来日后自己回顾,不详细
1、在项目的info.plist里面添加相应的键值对,将事先下载好的pdf拖到项目中,如图1.0和1.1,注意不是TARGETS下的info里面添加,在那里面添加会导致程序crash
![](https://img.haomeiwen.com/i3090071/dcebf18d29327636.png)
![](https://img.haomeiwen.com/i3090071/001e4831c7866d08.png)
2、进入程序,遵循代理<UIDocumentInteractionControllerDelegate>
设置全局变量或者属性
UIDocumentInteractionController *documentIntertactionController;
}
3、创建两个button,一个用来打开其他app,一个用来预览文件,如图3.1
![](https://img.haomeiwen.com/i3090071/ccaed658d27ba016.png)
4、打开的方法实现
// 打开
- (void)openUIDocument{
NSURL *pathUrl = [[NSBundle mainBundle] URLForResource:@"Steve" withExtension:@".pdf"];
documentIntertactionController = [UIDocumentInteractionController interactionControllerWithURL:pathUrl];
documentIntertactionController.delegate = self;
[self presentOptionsMenu];
}
- (void)presentOptionsMenu{
[documentIntertactionController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}
以下是上面方法的官方解释
application to pass to receiver (must be a plist object). default is nil.
// This is the default method you should call to give your users the option to quick look, open, or copy the document.
// Presents a menu allowing the user to Quick Look, open, or copy the item specified by URL.
// This automatically determines the correct application or applications that can open the item at URL.
// Returns NO if the options menu contained no options and was not opened.
// Note that you must implement the delegate method documentInteractionControllerViewControllerForPreview: to get the Quick Look menu item.
5、点击打开,如图5.1,这是模拟器的效果,在手机上还有其他软件,比如微信qq等等
![](https://img.haomeiwen.com/i3090071/26335d0b1b5b9bcd.png)
6、预览对应的代码
// 预览
- (void)previewUIDocument{
NSURL *pathUrl = [[NSBundle mainBundle] URLForResource:@"Steve" withExtension:@".pdf"];
documentIntertactionController = [UIDocumentInteractionController interactionControllerWithURL:pathUrl];
documentIntertactionController.delegate = self;
[self presentUIDocument];
}
- (void)presentUIDocument{
[documentIntertactionController presentPreviewAnimated:YES];
}
// 预览的时候需要加上系统的代理方法
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
7、点击预览,如图7.1
![](https://img.haomeiwen.com/i3090071/dc501709ed4f9bb8.png)
8、以上为加载本地存在的文件,下面介绍根据网址打开和预览
9、open网上链接,代码如下
- (void)openUIDocument{
[self openDocument];
}
- (void)openDocument{
[self pathUrl]; // 该方法在最下面
NSData *data = [NSData dataWithContentsOfURL:_pathUrl];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"Python核心编程.pdf"];
BOOL isWriteSuccess = [data writeToFile:filePath atomically:YES];
if(isWriteSuccess) {//success
NSURL *url = [NSURL fileURLWithPath:filePath];
[self previewPdfWithUrl:url];
[self presentOptionsMenu];
}
else{
NSLog(@"file not written");
}
}
- (void)previewPdfWithUrl:(NSURL*)url{
documentIntertactionController = [UIDocumentInteractionController interactionControllerWithURL:url];
documentIntertactionController.delegate = self;
}
- (void)presentOptionsMenu{
[documentIntertactionController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}
效果如图5.1
10、 预览,代码如下
- (void)previewUIDocument{
[self previewDocument];
}
- (void)previewDocument{
[self pathUrl];
NSData *data = [NSData dataWithContentsOfURL:_pathUrl];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"Python核心编程.pdf"];
BOOL isWriteSuccess = [data writeToFile:filePath atomically:YES];
if(isWriteSuccess) {//success
NSLog(@"%@",filePath);
NSURL *url = [NSURL fileURLWithPath:filePath];
[self previewPdfWithUrl:url];
[self presentUIDocument];
}
else{
NSLog(@"file not written");
}
}
- (void)previewPdfWithUrl:(NSURL*)url{
documentIntertactionController = [UIDocumentInteractionController interactionControllerWithURL:url];
documentIntertactionController.delegate = self;
}
- (NSURL *)pathUrl{
if (_pathUrl== nil) {
NSString *urlString = @"http://platform-test.lejuwanjia.com/api/v1/web/content/sale.order.picture/158/picture/Python核心编程.pdf";
NSString *urlStr = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
// _pathUrl = [NSURL URLWithString:urlStr];
_pathUrl = [NSURL URLWithString:urlStr];
NSLog(@"pathUrl:%@",_pathUrl);
}
return _pathUrl;
}
- (void)presentUIDocument{
[documentIntertactionController presentPreviewAnimated:YES];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
#pragma mark - url 中文格式化
+ (NSString *)strUTF8Encoding:(NSString *)str
{
/*! ios9适配的话 打开第一个 */
if ([[UIDevice currentDevice] systemVersion].floatValue >= 9.0)
{
return [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
}
else
{
return [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
}
效果如图7.1,这里是最简单的功能实现,没有考虑性能
简单的使用就暂时说到这里,功能远远不止这些,而且预览的时候要考虑手机是否支持等等,其他的注意事项以后再说
网友评论