UIWebView的基本用法

1、UIWebView的基础使用html

一、建立UIWebView:web

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

二、设置属性:浏览器

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

三、显示网页视图UIWebView:函数

[self.view addSubview:webView];ui

四、加载内容编码

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

也能够加载一个本地资源:lua

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"]];

 

五、导航

UIWebView类内部会管理浏览器的导航动做,经过goForward和goBack方法你能够控制前进与后退动做:

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

 

六、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的经常使用注意点。

 

2、IOS中UIWebView经常使用注意点:

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

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

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

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

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

四、为webView添加背景图片:

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

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

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

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

//第一种方法:
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]]];

七、将文件下载到本地址而后再用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];

八、处理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];}

相关文章
相关标签/搜索