WebView加载文件方式

转载内容来自:http://www.jianshu.com/p/e8c63551ef84

1. 加载html资源

  1. 直接加载url 网络上的网页
     [self.webView loadRequest:
     [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
  2. 加载本地的html文件

    1. 利用fileURLWithPath 根据fileurl加载

      NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@test.html",NSTemporaryDirectory()]];
      [self.webView loadFileURL:url allowingReadAccessToURL:url];
    2. 利用loadHTMLString 加载htmlString

      NSString *html = [NSString stringWithFormat:
      @"<html><head lang='en'><meta charset='UTF-8'></head><body>   
      <div style='margin-top: 100px'><h1>图片显示测试</h1><p style='width:580px;height: 539px;background: blue' onclick='picCallback()'>[站外图片上传中……(2)]</p><input type='button' value='CallCamera' onclick='OCModel.showAlertMsg(1,2)'></div>
      <script type='text/javascript'>var picCallback = function(photos) {alert('photos');}var shareCallback = function(){alert('success');}</script></body></html>"];
      // 用到的图片资源所在的路径
      NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@",NSTemporaryDirectory()]];
      [self.webView loadHTMLString:html baseURL:url];

      注意:baseURL参数要传入资源文件的目录,比如html中使用到了'test.jpg',在tmp目录下。

    3. - (nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL加载data 此方法可以加载大部分别的格式
      NSString *path = [NSString stringWithFormat:@"%@test.html",NSTemporaryDirectory()];
      NSData *Data = [NSData dataWithContentsOfFile:path];
      [self.webView loadData:Data MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];

2. 加载其他类型资源(doc,txt,pdf)

上述用到的fileURLWithPath- loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL也可以用来加载其他资源文件

  • fileURLWithPath

    txt文件
    NSString *path = [[NSBundle mainBundle] pathForResource:@"readme" ofType:@"txt"];
    [self.webView loadFileURL:[NSURL fileURLWithPath:path] allowingReadAccessToURL:[NSURL fileURLWithPath:path]];
    doc文件
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cwm.doc" ofType:nil];
    [self.webView loadFileURL:[NSURL fileURLWithPath:path] allowingReadAccessToURL:[NSURL fileURLWithPath:path]];
    这里要注意一点,如果doc只在main bundle中使用的,拖入工程文件的时候可能会读取不到对应的path。

    原因我们拉入其他资源文件(比如:图片、代码文件等)Xcode都会自动添加到target 的 "Build Phases" 下 "Copy Bundle Resources目录下,但是我们拉过来的文件这里是找不到的,可能是Xcode无法识别这类文件(当我们拉去过来时),所以我们要做的就是把刚才添加的doc文件加入该目录下

如下图


pdf文件

 NSString *path = [[NSBundle mainBundle] pathForResource:@"iOS" ofType:@"pdf"];
 [self.webView loadFileURL:[NSURL fileURLWithPath:path] allowingReadAccessToURL:[NSURL fileURLWithPath:path]];
  • - loadData:

    txt文件
      NSString *path = [NSString stringWithFormat:@"%@readme.txt",NSTemporaryDirectory()];
      NSData *Data = [NSData dataWithContentsOfFile:path];
      [self.webView loadData:Data MIMEType:@"text/plain" characterEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
    doc文件
      NSString *path = [NSString stringWithFormat:@"%@cwm.doc",NSTemporaryDirectory()];
      NSData *Data = [NSData dataWithContentsOfFile:path];
      [self.webView loadData:Data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" characterEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
    pdf文件
      NSString *path = [NSString stringWithFormat:@"%@iOS.pdf",NSTemporaryDirectory()];
      NSData *Data = [NSData dataWithContentsOfFile:path];
      [self.webView loadData:Data MIMEType:@"application/pdf" characterEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值