美文网首页集思广益iOS开发资料收集iOS -- Demo
iOS开发打开word、excel、ppt、txt、pdf文档(

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

作者: Courage_SC | 来源:发表于2016-05-20 13:41 被阅读5703次

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

    打开文件的方法:
    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.
    }
    

    相关文章

      网友评论

      本文标题:iOS开发打开word、excel、ppt、txt、pdf文档(

      本文链接:https://www.haomeiwen.com/subject/esrnrttx.html