一、UIWebView概述
类似Android中的WebView,使用一个视图控件在屏幕中加载显示网络链接、本地html文件、HTML代码等。
在iOS API中,提供了三种方法:用来实现这些功能:
- (void)loadRequest:(NSURLRequest *)request; - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL; - (void)loadData:(NSData *)data MIMEType:(NSString *) MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;第一种方法,直接加载网络链接。
NSString *path = @"http://theo2life.com"; NSURL *url = [[NSURL alloc] initWithString:path]; [webView loadRequest:[NSURLRequest requestWithURL:url]];第二种方法,载入本地hml文件或者HTML代码:
直接加载本地html文件“top.html”
NSString *resourcePath = [ [NSBundle mainBundle] resourcePath]; NSString *filePath = [resourcePath stringByAppendingPathComponent:@"top.html"]; NSString *htmlstring =[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; [webView loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]]];或者直接取出html文件的内容,变成字符串
NSString *HTMLData = @"<html> <head> <title>首页</title> <meta charset = "utf-8"> <meta name = "viewport" content = "width=device=width"> </head> <body> <h1>三个颜色</h1> <hr/> <h2>准备选择哪一件衣服?</h2> <ol> <li/><a href = "page1.html">红色衣服</a> <li/><a href = "page2.html">银色衣服</a> <li/><a href = "page3.html">黑色衣服</a> </ol> </body> </html>"; [webView loadHTMLString:HTMLData baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]]];第三种方法,相比第二种,给出了更详细的编码方式,除了可以加载HTML文件,也可以用来加载pdf/word/image/ppt等资源文件。
top.html
<html> <head> <title>首页</title> <meta charset = "utf-8"> <meta name = "viewport" content = "width=device=width"> </head> <body> <h1>三个颜色</h1> <hr/> <h2>准备选择哪一件衣服?</h2> <ol> <li/><a href = "page1.html">红色衣服</a> <li/><a href = "page2.html">银色衣服</a> <li/><a href = "page3.html">黑色衣服</a> </ol> </body> </html>
<html> <head> <title>Page1</title> <meta charset = "utf-8"> <meta name = "viewport" content = "width=device=width"> </head> <body> <h1>红色衣服</h1> <hr/> <h2>没有任何东西</h2> <ol> <li/><a href = "top.html">返回</a> </ol> </body> </html>
<html> <head> <title>Page2</title> <meta charset = "utf-8"> <meta name = "viewport" content = "width=device=width"> </head> <body> <h1>银色衣服</h1> <hr/> <h2>两件衣服</h2> <ol> <li/><a href = "page1.html">红色衣服</a> <li/><a href = "page3.html">黑色衣服</a> <li/><a href = "top.html">返回</a> </ol> </body> </html>
<html> <head> <title>Page3</title> <meta charset = "utf-8"> <meta name = "viewport" content = "width=device=width"> </head> <body> <h1>黑色衣服</h1> <hr/> <h2>有一条黑色的围巾</h2> <ol> <li/><a href = "top.html">返回</a> </ol> <form action="document.title"> <input type = "submit" value = "执行js" /> </form> </body> </html>
#import "ViewController.h" @interface ViewController () @end UIWebView *webView; @implementation ViewController -(void)dealloc{ if([webView isLoading]){ [webView stopLoading]; } [webView release]; [super dealloc]; } //加载视图 - (void)viewDidLoad { [super viewDidLoad]; self.title = @"webview"; webView = [[UIWebView alloc] init]; //webView.delegate = self; webView.frame = self.view.bounds; webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [self.view addSubview:webView]; } /*私有方法*/ -(void)loadHTMLFile:(NSString *)path{ NSString *resourcePath = [ [NSBundle mainBundle] resourcePath]; NSString *filePath = [resourcePath stringByAppendingPathComponent:path]; NSString *htmlstring =[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; [webView loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]]]; NSLog(@"resourcesPath:%@", resourcePath); NSLog(@"filePath: %@",filePath); NSLog(@"htmlString: %@",htmlstring); } //继承的方法 -(BOOL)webView: (UIWebView *)webView shouldStartLoadWithRequest:(nonnull NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ //触摸连接后,进入href属性指定的下一个页面 if(UIWebViewNavigationTypeLinkClicked == navigationType){ NSString *url = [[request URL] path]; [self loadHTMLFile:url]; return FALSE; //对链接进行了拦截处理,返回false }else if(UIWebViewNavigationTypeFormSubmitted == navigationType){ NSString *url = [[request URL] path]; NSArray *component = [url pathComponents]; NSString *resultString = [webView stringByEvaluatingJavaScriptFromString:[component lastObject]]; //显示对话框 UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"title" message:resultString preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"Cancle" style:UIAlertActionStyleCancel handler: ^(UIAlertAction *action){ NSLog(@"Click the cancle button."); }]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler: ^(UIAlertAction *action){ NSLog(@"Click the ok button."); }]; [controller addAction:cancleAction]; [controller addAction:otherAction]; [self presentViewController:controller animated:YES completion:nil]; return FALSE; } return TRUE; } //画面显示时,自动调用的方法。显示top.html -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self loadHTMLFile:@"top.html"]; } -(void)webViewDidStartLoad: (UIWebView *)webView{ NSLog(@"WebView did start load."); [self updateControlEnabled]; } -(void)webViewDidFinishLoad: (UIWebView *)webView{ NSLog(@"WebView did finish load."); [self updateControlEnabled]; } -(void)webView: (UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{ NSLog(@"Did failed load woth error: %li", error.code); NSLog(@"%@", error.localizedDescription); [self updateControlEnabled]; } -(void)updateControlEnabled{ if([webView isLoading]){ NSLog(@"WebView is loading"); }else{ NSLog(@"WebView has finished loading or error"); } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end执行效果:
未完待续...