UIWebView 使用
基于手机 网络环境问题 在UIWebView控件使用的时候,程序习惯添加UIActivityIndicatorView用以控制UIWebview的加载等待,在等待的时间内用户不允许操作,所以我们还要在当前的UIWebview上添加一个透明的view(opaqueview)用以防止用户点击,然后把 UIActivityIndicatorView添加到我们自己定义的view(opaqueview)上。这样当网页加载开始的时候我们把 UIActivityIndicatorView打开,当网页加载完毕的时候我们可以取消UIActivityIndicatorView,从而实现了对 UIWebView的加载等待。
一.UIWebView加载的时候通常有几种方法
1.loadRequest:
2.loadHTMLString:string baseURL:
3.loadData:MIMEType:textEncodingName:baseURL:
其中baseURL 是指基准的url 是一个绝对的地址,程序要用到的其他资源就可以根据这个基准地址进行查找而不用再次定位到绝对地址;
二.UIWebView中几个重要的函数
1.- (void )webViewDidStartLoad:(UIWebView *)webView 网页开始加载的时候调用
2.- (void )webViewDidFinishLoad:(UIWebView *)webView 网页加载完成的时候调用
3.-(BOOL )webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType )navigationType
当程序以UIWebView加载方式1进行加载的时候就会调用到此函数,然后执行webViewDidStartLoad函数,所以我们可以在此函数中进行一些请求解析,URL地址分析的工作。
4.- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
是一个可选的函数,如果页面加载失败可以根据不同的错误类型反馈给用户不同的信息
三.代码实现
1.在.h文件中加载声明UIWebviewDelegate协议
声明WebView
2.实现UIWebView
WebView = [[ UIWebView alloc] initWithFrame: CGRectMake(x,x,x,x )];
[ WebView setUserInteractionEnabled: YES ]; //是否支持交互
[ WebView setDelegate: self ]; //委托
[ WebView setOpaque: NO ]; //透明
[ self . view addSubview : WebView]; //加载到自己的view
url = [[ NSURL alloc ] initWithString :@"http:"];
[ WebView loadRequest:[ NSURLRequest requestWithURL: url ]]; //笔者习惯采用loadRequest方式,你可以采用其他方式
opaqueview = [[ UIView alloc] initWithFrame: CGRectMake(x , x , x , x )]; //opaqueview 需要在.h文件中进行声明 用以做UIActivityIndicatorView的容器view;
activityIndicator = [[ UIActivityIndicatorView alloc] initWithFrame: CGRectMake( x , x , x , x )];//需要在.h文件中进行声明
[ activityIndicator setCenter : opaqueview. center ];
[ activityIndicator setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleWhite]; //颜色根据不同的界面自己调整
[ opaqueview setBackgroundColor:[ UIColor blackColor]];
[ opaqueview setAlpha: 0.6 ];
[ self . view addSubview : opaqueview];
[ opaqueview addSubview : activityIndicator];
接下来添加
- (void )webViewDidFinishLoad:(UIWebView *)webView {
[activityIndicator stopAnimating];
opaqueview.hidden = YES ;
}
- (void )webViewDidStartLoad:(UIWebView *)webView {
[ activityIndicator startAnimating ];
opaqueview.hidden = NO ;
}
这样就简单的实现了UIWebView控件的加载使用,不同的页面均有加载的等待。
*****************************************************************************
*****************************************************************************
NSLog(@"loading");
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blueColor];
NSLog(@"self view");
// view orientation rotation
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view = contentView;
//设置属性
//自动调整视图大小
self.view.autoresizesSubviews = NO;
//
//创建一个层用来放webview
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webFrame.origin.y -= 20.0;
//
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
self.webView = aWebView;
//缩放
aWebView.scalesPageToFit = NO;
//自动调整大小
aWebView.autoresizesSubviews = NO;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//[aWebView setDelegate:self];
NSURL *aURL = [NSURL URLWithString:@"http://192.168.1.120/emenu/?tp=tp4"];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL];
//发送请求
[aWebView loadRequest:aRequest];
//把webview添加到内容视图
[contentView addSubview:webView];
[aWebView release];
[contentView release];
1.只有使用loadRequest:加载网页,才能对之后的链接操作做goBack,goForward操作,即canGoBack,canGoForward才有可能返回YES.
使用loadHTMLString,loadData都不可以.
2.把要请求的地址直接交于UIWebView loadRequest,它在请求时,也会把这个地址回调到shouldStartLoadWithRequest代理方法中,询问是否由浏览器自己请求.
3.goBack也会请求页面,也会回调代理方法.
WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 320, 400)];
[WebView setUserInteractionEnabled:NO];
[WebView setBackgroundColor:[UIColor clearColor]];
[WebView setDelegate:self];
[WebView setOpaque:NO];//使网页透明
//加载url数据
NSString *path = @"http://www.baidu.com";
NSURL *url = [NSURL URLWithString:path];
[WebView loadRequest:[NSURLRequest requestWithURL:url]];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[view setTag:103];
activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
[activityIndicator setCenter:view.center];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
[view setBackgroundColor:[UIColor blackColor]];
[view setAlpha:0.8];
[self.view addSubview:view];
[view addSubview:activityIndicator];
[view release];
[self.view addSubview:WebView];
[WebView release];
//加载本地html数据
NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];
NSString *filePath = [resourcePath stringByAppendingPathComponent:@"0.html"];
NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[WebView loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[self.view addSubview:WebView];
[WebView release];
//加载完数据
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[WebView stringByEvaluatingJavaScriptFromString:@"{/
var a = document.getElementsByTagName_r("a"); /
for (var i=0; i<a.length; i++) /
a[i].target = "_self";/
}"];
[activityIndicator stopAnimating];
UIView *view = (UIView *)[self.view viewWithTag:103];
[view removeFromSuperview];
}
//开始加载数据
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
return YES;
}
//加载网页图片
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
NSString* theURLString = @"http://www.sdlawyer.net/blog/UploadFiles/2008-1/231416859412.jpg";
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:theURLString]];
[imageView setImage:[UIImage imageWithData:data]];
[self.view addSubview:imageView];
[imageView release];
//加载本地图片
NSString *HtmlData=[NSString stringWithFormat:@"<html><head><style>body{background-color:transparent;font-family:Arial;}</style></head><body><p>%@</p><p><img src=%@ alt="picture" title="picture" /></p></body></html>",[data.ImageDataArray objectAtIndex:page], [data.ImageDataArray objectAtIndex:page]];
[BrowseWebView loadHTMLString:HtmlData baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
-(void)Pre:(id)sender{
[WebView goBack];//返回上一记录
}
-(void)NextBtn:(id)sender{
[WebView goForward];//返回下一记录
}
Then back in your Objective-C code:
- (BOOL)webView:(UIWebView *)webView2
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestString = [[request URL] absoluteString];
NSArray *components = [requestString componentsSeparatedByString:@":"];
if ([components count] > 1 &&
[(NSString *)[components objectAtIndex:0] isEqualToString:@"myapp"]) {
if([(NSString *)[components objectAtIndex:1] isEqualToString:@"myfunction"])
{
NSLog([components objectAtIndex:2]); // param1
NSLog([components objectAtIndex:3]); // param2
// Call your method in Objective-C method using the above...
}
return NO;
}
return YES; // Return YES to make sure regular navigation works as expected.
}
从本地加载图片
SString *boundle = [[NSBundle mainBundle] resourcePath];
web1 loadHTMLString:[NSString stringWithFormat:@"<img src='0001.png'/>"] baseURL:[NSURL fileURLWithPath:boundle]];
u8232 从网页加载图片并让图片在规定长宽中缩小
cf0 [cell.img loadHTMLString:[NSString stringWithFormat:@"<html><body><img src='%@' height='90px' width='90px'></body></html>",goodsInfo.GoodsImg] baseURL:nil];
webview上通过javascript获取里面的数据,如果只是发送了一个连接请求获取到源码以后可以用正则表达式进行获取数据
cf0 NSString *javaScript1 = @"document.getElementsByName('.u').item(0).value";
SString *javaScript2 = @"document.getElementsByName('.challenge').item(0).value";
SString *strResult1 = [NSString stringWithString:[theWebView stringByEvaluatingJavaScriptFromString:javaScript1]];
SString *strResult2 = [NSString stringWithString:[theWebView stringByEvaluatingJavaScriptFromString:javaScript2]];