UIWebView(一):加载html

来源:互联网 发布:2017淘宝销售排行 编辑:程序博客网 时间:2024/05/16 10:21

UIWebView 加载html 分为以下三种情况:

1,加载网络html:

NSURL *url = [NSURL URLWithString:@"http://localhost:8080/jmDemo/index.html"];NSURLRequest *request = [NSURLRequest requestWithURL:url];[_webView loadRequest:request];

2,加载本地html,文件在文件项目中;

 NSURL *baseURL = [NSURL fileURLWithPath:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"assets"]];NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"htm" inDirectory:@"assets"];NSString *htmlString = [[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil] autorelease];    [_webView loadHTMLString:htmlString baseURL:baseURL];

注意:此处将html 文件夹拖入工程目录时要选择,见下图:
这里写图片描述

拖进去后在工程目录显示蓝色的文件夹:
这里写图片描述

这样拖进去的html文件夹,在打包成.app工程包后仍会保持原来的文件结构,html文件在app运行时引用图片等相关资源不会出问题。
这里写图片描述

3,加载本地html,文件在文件Document中;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];NSString *path = [documentsDirectory stringByAppendingPathComponent:@"index.html"];NSURL *url = [NSURL fileURLWithPath:path];NSURLRequest *request = [NSURLRequest requestWithURL:url];[_webView loadRequest:request];
0 0
原创粉丝点击