IOS学习之WebView加载本地HTML代码或网络资源

新建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];
    }
}


方法3:采用
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”,图标为蓝色


此时即可得到结果~




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值