集思广益iOS开发资料收集iOS -- Demo

iOS开发打开word、excel、ppt、txt、pdf文档(

2016-05-20  本文已影响5703人  Courage_SC

非常感谢大家对我的关注!!!!

打开文件的方法:
1.获取文件的沙盒路径path
2.将path路径转化URL
3.用webView显示出来

#import "WebViewController.h"

@interface WebViewController ()<UIWebViewDelegate>
@property(nonatomic, strong)UIWebView *webView;
@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    _webView.delegate = self;
    _webView.scalesPageToFit = YES;
    [self.view addSubview:_webView];
    
    //获取文件路径
    NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //获取document文件夹路径
    NSString *documents = [array lastObject];
    //拼接绝对路径
    NSString *documentPath = [documents stringByAppendingPathComponent:@"myReport/file"];
    //存数据的具体文件夹
    // [manager createDirectoryAtPath:documentPath withIntermediateDirectories:YES attributes:nil error:nil];
    //得到文件名
    NSArray *fileNameArray = [manager subpathsAtPath:documentPath];

    NSString *path = [NSString stringWithFormat:@"%@/%@", documentPath, fileNameArray[0]];

    //加载文件
    [self loadDocument: path inView:_webView];
    
}
#parm mark - 加载文件
- (void)loadDocument:(NSString *)documentPath inView:(UIWebView *)webView{
   // NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
    NSURL *url = [NSURL fileURLWithPath:documentPath];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
上一篇 下一篇

猜你喜欢

热点阅读