UIWebView的加载方法
1.UIWebView的loadRequest:方法
__weak typeof (self)weakSelf=self;
NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
NSString *strUrl=_webViewImageURL;
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[weakSelf.webView loadRequest:request];
[MBProgressHUD showHUDAddedTo:_webView animated:YES];
}];
}];
queue=[[NSOperationQueue alloc]init];
[queue addOperation:operation];
2.UIWebView的loadHTMLString::方法
将本地html文件内容嵌入webView
NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];
NSString *filePath = [resourcePath stringByAppendingPathComponent:@"test.html"];
NSString *htmlstring =[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]]];
对于第二种加载方式,可以使用模板引擎渲染HTML界面
UIWebView通过
- (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
关于GRMustache引擎
1.它将模板内容放在另一个单独的文件中,方便日后更改
2.将原来一stringFormat格式固定的%@ 替换为{{ name }}形式
模板调整后变成了如下内容(文件名为template.html)
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>{{ name }}</H1>
<P>{{ content }}</P>
</BODY>
</HTML>
然后我们在代码中将该文件读取到内存中(把fileName和bundlePath 用stringByAppendingPathComponent拼接起来,用stringWithContentsOfFile:读取 文件),
再使用GRMustache的renderObject方法生成渲染后的HTML内容.
然后让UIWebView通过loadHTMLString:方法加载