IOS开发iOSiOS Developer

UIWebView加载本地HTML文件

2017-04-12  本文已影响939人  Onegeng

一、 WebView简单介绍


webView加载数据的方式文档给出了三种:

- (void)loadRequest:(NSURLRequest *)request;
- (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;

  1. - (void)loadRequest:(NSURLRequest )request;
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
[self.view addSubview:webView];
[webView loadRequest:request];

加载本地HTML

NSString *path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"html"];
NSURL* url = [NSURL  fileURLWithPath:path];//创建URL
NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
[webView loadRequest:request];//加载
  1. - (void)loadHTMLString:(NSString )string baseURL:(nullable NSURL)baseURL;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"html/start" ofType:@"html"];
    NSString *htmlString1 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
    NSString *urlStr = [NSString stringWithFormat:@"%@/html",[[NSBundle mainBundle] bundlePath]];
    NSURL *url = [NSURL fileURLWithPath:urlStr isDirectory:YES];
      [self.webView loadHTMLString:htmlString1 baseURL:url];
    NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
    NSString *basePath = [NSString stringWithFormat:@"%@/html",mainBundlePath];
    NSURL *baseUrl = [NSURL fileURLWithPath:basePath isDirectory:YES];
    NSString *htmlPath = [NSString stringWithFormat:@"%@/start.html",basePath];
    NSString *htmlString = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
    [self.webView loadHTMLString:htmlString baseURL:baseUrl];

注意:baseURL因为html中一些资源的寻找需要到上个目录中寻找,因此要写

NSString *path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"html"];

不太了解Creat groups和Creat folder reference引入文件的同学,下面详细介绍区别.

/**
 * data: 所加载文件的数据
 * MIMEType:文件类型
 * textEncodingName: 编码类型
 * baseURL: 资源路径
*/

webView的代理方法:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;

oc的方法名称就像句子一样,清晰明了的表达了方法的作用,因此代理我就不在啰嗦了!

二、加载本地HTML


总结: Creat groups和Creat folder reference的区别

其它差异欢迎补充

Bundle介绍

[[NSBundle mainBundle] pathForResource:@"someFileName" ofType:@"yourFileExtension"]; 
[YourViewController initWithNibName:"YourViewController" bundle:nil];

通过一下方法可以获取
[NSBundle mainBundle];

NSString *bundlePath =  [[NSBundle mainBundle] pathForResource:@"文件名字" ofType:@"bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
 NSString *resourcePath = [resourceBundle pathForResource:@"" ofType:@""];
 UIImage *image = [UIImage imageWithContentsOfFile:resourcePath];
这里有个小tap需要主要下 就是加载图片为什么用 imageWithContentsOfFile而不用imagName?

bundle居多是用来存储第三方库的资源,这样做是为了防止引入的库内的资源名字与项目名字冲突,这样可以很好的防止资源名字重复问题,但是如果你用了imagName加载图片,图片可能就会无法区分。

整体结构 HTML结构
NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];//文件根路径
NSString *basePath = [NSString stringWithFormat:@"%@/html",mainBundlePath];//获取目标html文件夹路径
NSURL *baseUrl = [NSURL fileURLWithPath:basePath isDirectory:YES];//转换成url
NSString *htmlPath = [NSString stringWithFormat:@"%@/start.html",basePath];//目标 文件路径
NSString *htmlString = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];//把html文件转换成string类型
[self.webView loadHTMLString:htmlString baseURL:baseUrl];//加载

三、加载本地HTML文件内容适配问题


self.webView.scalesPageToFit = YES;

因此我们可以通过代理直接修改网页的比例,可以在代理中通过下面方法来调整网页的比例大小:

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
        [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom=0.52"];
}

代码地址:https://github.com/onegeng/Html-App
本文内容多有不全,后续将会继续补充添加,进一步完善

欢迎大家的意见和指导.如果有什么问题请留言或者给我发邮件onegeng@aliyun.com

本文主要参考文章.

上一篇下一篇

猜你喜欢

热点阅读