序言:
灵活的触摸处理也是UIWebView中的强项。对此,我们也可以开发基于HTML代码的应用程序。
正文:
第一:
我们要创建一个UIWebView的对象。代码:
UIWebView*_ZSJwebView;
我们初始化我们创建的UIwebView的对象_ZSJwebView,代码如下;
_ZSJwebView=[[UIWebView alloc]initWithFrame:self.view.frame]; _ZSJwebView.delegate=self; _ZSJwebView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; // _ZSJwebView.dataDetectorTypes=UIDataDetectorTypeAll; [self.view addSubview:_ZSJwebView];
我们写加载HTML文件的私有方法、。
//读入指定的HTML文件的私有方法 -(void)loadHTMLField:(NSString*)path { NSArray*compArr=[path pathComponents]; NSLog(@"%@",compArr); NSString*resourStr=[compArr lastObject]; NSLog(@"%@",resourStr); NSString*absolutepath; if (absolutepath =[[NSBundle mainBundle] pathForResource:resourStr ofType:nil]) { NSData*data=[NSData dataWithContentsOfFile:absolutepath]; [_ZSJwebView loadData:data MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:nil]; }else { NSLog(@"%@ not font",resourStr); } }
我们处理触摸后的跳转。代码:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ //触摸链接后,进入href属性为URL的下一个画面。 if ( UIWebViewNavigationTypeLinkClicked==navigationType) { NSString*url=[[request URL] path]; [self loadHTMLField:url]; return FALSE; } return TRUE; }
我们要设定首次,加载的网页。代码:
//画面显示后,首先显示top。htm -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self loadHTMLField:@"top.htm"]; }
我们还要写两个HTML 文件。
文件一、
[top.htm]
<html> <head> <title>首页</title> <meta charset=@"utf-8"> <meta name="岁月风沙" content="width=device-width"/> </head> <body> <h1>三个美女</h1> <hr/> <h2>准备要哪个美女</h2> <o1> <li /><a href="page1.htm">刘亦菲</a> <li /><a href="page2.htm">范冰冰</a> <li /><a href="page3.htm">何洁</a> </o1> </body> </html>文件二、
[page1.htm]
<html> <head> <title>刘亦菲美女</title> <meta charset=@"utf-8"> <meta name="岁月风沙" content="width=device-width"/> </head> <body> <h1>刘亦菲</h1> <hr/> <h2> 哪个美女,你都别想要!</h2> <o1> <li /><a href="top.htm">返回</a> </o1> </body> </html>
效果展示:
完整代码:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIWebViewDelegate> { UIWebView*_ZSJwebView; UIActivityIndicatorView*_ZSJActivity; } @end
#import "ViewController.h" #import "Mylabel.h" @implementation ViewController -(void)viewDidLoad{ _ZSJwebView=[[UIWebView alloc]initWithFrame:self.view.frame]; _ZSJwebView.delegate=self; _ZSJwebView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; // _ZSJwebView.dataDetectorTypes=UIDataDetectorTypeAll; [self.view addSubview:_ZSJwebView]; _ZSJActivity=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)]; UIBarButtonItem*indicator=[[UIBarButtonItem alloc]initWithCustomView:_ZSJActivity]; UIBarButtonItem*adjustment=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; NSArray*buttons=[NSArray arrayWithObjects:adjustment,indicator,adjustment, nil]; [self setToolbarItems:buttons animated:YES]; } //读入指定的HTML文件的私有方法 -(void)loadHTMLField:(NSString*)path { NSArray*compArr=[path pathComponents]; NSLog(@"%@",compArr); NSString*resourStr=[compArr lastObject]; NSLog(@"%@",resourStr); NSString*absolutepath; if (absolutepath =[[NSBundle mainBundle] pathForResource:resourStr ofType:nil]) { NSData*data=[NSData dataWithContentsOfFile:absolutepath]; [_ZSJwebView loadData:data MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:nil]; }else { NSLog(@"%@ not font",resourStr); } } -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ //触摸链接后,进入href属性为URL的下一个画面。 if ( UIWebViewNavigationTypeLinkClicked==navigationType) { NSString*url=[[request URL] path]; [self loadHTMLField:url]; return FALSE; } return TRUE; } //画面显示后,首先显示top。htm -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self loadHTMLField:@"top.htm"]; } @end
【top.htm】 <html> <head> <title>首页</title> <meta charset=@"utf-8"> <meta name="岁月风沙" content="width=device-width"/> </head> <body> <h1>三个美女</h1> <hr/> <h2>准备要哪个美女</h2> <o1> <li /><a href="page1.htm">刘亦菲</a> <li /><a href="page2.htm">范冰冰</a> <li /><a href="page3.htm">何洁</a> </o1> </body> </html>
【page1.htm】 <html> <head> <title>刘亦菲美女</title> <meta charset=@"utf-8"> <meta name="岁月风沙" content="width=device-width"/> </head> <body> <h1>刘亦菲</h1> <hr/> <h2> 哪个美女,你都别想要!</h2> <o1> <li /><a href="top.htm">返回</a> </o1> </body> </html>