现在的位置: 首页 > 综合 > 正文

UIWebView的三种加载方式

2018年02月03日 ⁄ 综合 ⁄ 共 713字 ⁄ 字号 评论关闭

1、直接给出url地址即可将web content载入。

NSString *path = @"http://theo2life.com";  
NSURL *url = [[NSURL alloc] initWithString:path];  
[self.webView loadRequest:[NSURLRequest requestWithURL:url]]; 

2、将本地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]]];  

3.如果不从html文件载,直接加载NSString:

NSString *HTMLData = @"<img src=\"test.png\" />hello web";  
[self.webView loadHTMLString:HTMLData baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]];  

抱歉!评论已关闭.