新建SingleViewApplication应用,命名WebViewSample, 在storyboard中拖3个Button,定义动作,再拖一个WebView,然后创建他们的弱链接输出口,得到如下的代码:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webViewe;
- (IBAction)testLoadHTMLString:(id)sender;
- (IBAction)testLoadData:(id)sender;
- (IBAction)testLoadRequest:(id)sender;
@end
布局如下图:
在ViewVontroller.m中编辑动作事件:
方法1:采用
<span style="color: rgb(54, 46, 43);">loadHTMLString方法加载本地HTML(</span><span style="color:#ff0000;">注:注释掉的方法也可以使用</span><span style="color:#362e2b;">)</span><pre name="code" class="html" style="color: rgb(54, 46, 43);">是<span style="color:#ff6666;">同步加载方式</span>
- (IBAction)testLoadHTMLString:(id)sender {
// NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
// NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
// NSError *error = nil;
//
// NSString *html = [[NSString alloc] initWithContentsOfFile:htmlPath encoding: NSUTF8StringEncoding error:&error];
//
// if (error == nil) {//数据加载没有错误情况下
// [self.webViewe loadHTMLString:html baseURL:bundleUrl];
// }
NSBundle *bundle = [NSBundle mainBundle];
NSString *resPath = [bundle resourcePath];
NSString *filePath = [resPath stringByAppendingPathComponent:@"index.html"];
[self.webViewe loadHTMLString:[NSString stringWithContentsOfFile:filePath] baseURL:[NSURL fileURLWithPath:[bundle bundlePath]]];
}
方法2:采用
loadData的方法加载本地文件的数据;是<span style="color:#ff6666;">同步加载方式</span>
- (IBAction)testLoadData:(id)sender {
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSError *error = nil;
NSData *htmlData = [[NSData alloc] initWithContentsOfFile: htmlPath];
if (error == nil) {//数据加载没有错误情况下
[self.webViewe loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8"baseURL: bundleUrl];
}
}
loadRequest的方法,是<span style="color:#ff0000;background-color: rgb(255, 255, 255);">异步加载方式</span>
- (IBAction)testLoadRequest:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://www.51work6.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webViewe loadRequest:request];
self.webViewe.delegate = self;
}
#pragma mark -- UIWebDelegate
-(void) webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"%@", [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"]);
}
注:在加载本地HTML文件代码时,需要在本地文件夹中有index.html文件存在。步骤:
1、新建一个html文件,将下面代码拷贝进去:
<HEAD>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
</HEAD>
<BODY>
<H1>
WebView: 帮助</H1>
<UL>
<LI><a href="#create">Create</a></LI>
<LI><a href="#edit">Edit</a></LI>
<LI><a href="#delete">Delete</a></LI>
<LI><a href="#mail">Mail</a></LI>
</UL>
<p>
<a name="create"></a>
<strong>Create:</strong><BR>
You can create new notes by selecting the + button in the upper right hand side of the main list of notes.
This will create a new blank note for editing.
</p>
<p>
<a name="edit"></a>
<strong>Edit:</strong><BR>
You can edit notes by selecting a note from the list on the main list page. This will bring your existing note
up in the editor. You can edit the note and save your changes.
</p>
<p>
<a name="delete"></a>
<strong>Delete:</strong><BR>
You can delete notes by selecting the edit button in the upper left corner on the main list page. This will enable
the standard list editing function to allow you to delete notes.
</p>
<p>
<a name="mail"></a>
<strong>Mail:</strong><BR>
You can mail notes by selecting the action button in the lower left corner on the notes view page. This will bring
up an action list that lets you select Mail Note from which point you can address and send your note.
</p>
<p>
</BODY>
2、将此文件加载到
WebViewSample中,与ViewVontroller.m同一目录:
右键组名WebViewSample,选中Add Files To “WebViewSample”,下面选中Create Groups,点击Add,即可。
注:create Groups是组的概念,此时引用路径为“index.html”,图标为黄色;
create Folder reference 是文件夹的概念,引用路径是“xxx文件夹/index.html”,图标为蓝色;
此时即可得到结果~