iOS 加载显示 Word PDF Excel

来源:互联网 发布:田岛绣花软件9.0 编辑:程序博客网 时间:2024/06/05 11:18

通常有三种方式 

1.使用webView

2.使用自带的CLPreviewController类

3.绘制PDF文档


这里重点说明前两种

1.使用webView  只需要有文件的URL就可以了 先上代码


#import "PDFViewController.h"

@interface PDFViewController ()

@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end

@implementation PDFViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView.scalesPageToFit = YES;
    NSString *documentLocation =  [[NSBundle mainBundle]  pathForResource:@"cs" ofType:@"pdf"];
    NSURL *myDocument = [NSURL fileURLWithPath:documentLocation];
    NSURLRequest *request = [NSURLRequest requestWithURL:myDocument];
    [self.webView loadRequest:request];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

使用webView有缺点 就是当文件比较大的时候 会有点慢 并且默认回事黑色的背景


2.使用自带的CLPreviewController类

苹果自带的CLPreviewController  这个类是 看一点加载一点  代码

第一步 导入QuartzCore QuartLook 这两个包

第二步 #import <QuickLook/QuickLook.h>  添加代理

第三步 实现代理方法

#import "WordViewController.h"

@interface WordViewController ()<QLPreviewControllerDataSource>

@property (nonatomic,strong) QLPreviewController *qlpreviewController;

@end

@implementation WordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _qlpreviewController = [[QLPreviewController alloc] init];
    _qlpreviewController.dataSource = self;
}

#pragma mark - qlpreViewdataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}
//返回一个需要加载文件的URL
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller
                     previewItemAtIndex:(NSInteger)index {
    NSString *documentLocation = [[NSBundle mainBundle]
                                  pathForResource:@"word" ofType:@"docx"];
    NSURL *myQLDocument = [NSURL fileURLWithPath:documentLocation];
    
    return myQLDocument;
}
//点击这个按钮开始打开文件  两种建议用push  模态过去的有系统自带的一些按钮 自己试验了
- (IBAction)openWord:(UIButton *)sender {
    
//    [self presentViewController:_qlpreviewController animated:YES completion:nil];
    [self.navigationController pushViewController:_qlpreviewController animated:YES];
}

3.绘制PDF

后面补上 iOS 编辑Word 文件的例子 怎么能够显示手机的一些文件!!!!!

0 0