UIWebView的基本用法

时间:2023-03-09 03:55:18
UIWebView的基本用法

一、UIWebView的基础使用

1、创建UIWebView:

CGRect bouds = [[UIScreen manScreen]applicationFrame]; 
UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];

2、设置属性:

webView.scalespageToFit = YES;//自动对页面进行缩放以适应屏幕 
webView.detectsPhoneNumbers = YES;//自动检测网页上的电话号码,单击可以拨打

3、显示网页视图UIWebView:

[self.view addSubview:webView];

4、加载内容

NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"];//创建URL 
NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest 
[webView loadRequest:request];//加载

也可以加载一个本地资源:

NSURL* url = [NSURL fileURLWithPath:filePath];//创建URL 
NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest 
[webView loadRequest:request];//加载

UIWebView 还支持将一个NSString对象作为源来加载。你可以为其提供一个基础URL,来指导UIWebView对象如何跟随链接和加载远程资源:

[webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];

5、导航

UIWebView类内部会管理浏览器的导航动作,通过goForward和goBack方法你可以控制前进与后退动作:

[webView goBack]; 
[webView goForward]; 
[webView reload];//重载 
[webView stopLoading];//取消载入内容

6、UIWebViewDelegate委托代理

UIWebView支持一组委托方法,这些方法将在特定时间得到通知。要使用这些方法,必须先设定webView的委托:

webView.delegate = self;

下面每个委托方法的第一个参数都是指向一个UIwebview的指针,因此你可以将一个委托用于多个网页视图。

-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*) reuqest navigationType: (UIWebViewNavigationType)navigationType;//当网页视图被指示载入内容而得到通知。应当返回YES,这样会进行加载。通过导航类型参数可以得到请求发起的原因,可以是以下任意值: 
UIWebViewNavigationTypeLinkClicked 
UIWebViewNavigationTypeFormSubmitted 
UIWebViewNavigationTypeBackForward 
UIWebViewNavigationTypeReload 
UIWebViewNavigationTypeFormResubmitted 
UIWebViewNavigationTypeOther

UIWebView控件加载网页的监听函数方法:

-(void)webViewDidStartLoad:(UIWebView*)webView ;//当网页视图已经开始加载一个请求后,得到通知。 
-(void)webViewDidFinishLoad:(UIWebView*)webView ;//当网页视图结束加载一个请求之后,得到通知。 
-(void)webView:(UIWebView*)webView DidFailLoadWithError:(NSError*)error;//当在请求加载中发生错误时,得到通知。会提供一个NSSError对象,以标识所发生错误类型。

以上是IOS中UIWebView的基础使用要点详解,接下来一些UIWebView的常用注意点。

二、IOS中UIWebView常用注意点:

1、与UIWebView进行交互,调用web页面中的需要传参的函数时,参数需要带单引号,或者双引号(双引号需要进行转义在转义字符前加\),在传递json字符串时不需要加单引号或双引号:

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *sendJsStr=[NSString stringWithFormat:@"openFile(\"%@\")",jsDocPathStr];
[webView stringByEvaluatingJavaScriptFromString:sendJsStr];
}

2、在该代理方法中判断与webView的交互,可通过html里定义的协议实现:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType

3、只有在webView加载完毕之后在能够调用对应页面中的js方法。(对应方法如第1条).

4、为webView添加背景图片:

approvalWebView.backgroundColor=[UIColor clearColor];
approvalWebView.opaque=NO;//这句话很重要,webView是否是不透明的,no为透明 在webView下添加个imageView展示图片就可以了

5、获取webView页面内容信息:

NSString *docStr=[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];//获取web页面内容信息,此处获取的是个json字符串
SBJsonParser *parserJson=[[[SBJsonParser alloc]init]autorelease];
NSDictionary *contentDic=[parserJson objectWithString:docStr];//将json字符串转化为字典

6、 加载本地文件的方法:

//第一种方法:
NSString* path = [[NSBundle mainBundle] pathForResource:name ofType:@"html" inDirectory:@"mobile"];//mobile是根目录,name是文件名称,html是文件类型
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]]; //加载本地文件
//第二种方法:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath]; 
NSString *filePath = [resourcePath stringByAppendingPathComponent:@"mobile.html"]; 
NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 
[uiwebview loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

7、将文件下载到本地址然后再用webView打开:

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
self.filePath = [resourceDocPath stringByAppendingPathComponent:[NSString stringWithFormat:@"maydoc%@",docType]];
NSData *attachmentData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:theUrl]];
[attachmentData writeToFile:filePath atomically:YES];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[attachmentWebView loadRequest:requestObj];
//删除指定目录下的文件
NSFileManager *magngerDoc=[NSFileManager defaultManager];
[magngerDoc removeItemAtPath:filePath error:nil];

8、处理webView展示txt文档乱码问题:

if ([theType isEqualToString:@".txt"]) 
{
//txt分带编码和不带编码两种,带编码的如UTF-8格式txt,不带编码的如ANSI格式txt
//不带的,可以依次尝试GBK和GB18030编码
NSString* aStr = [[NSString alloc] initWithData:attachmentData encoding:NSUTF8StringEncoding];
if (!aStr) 
{
//用GBK进行编码
aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000632];
}
if (!aStr) 
{
//用GBK编码不行,再用GB18030编码
aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000631];
}