一.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控件的加载使用,不同的页面均有加载的等待。
****js调用iphone中的方法的实现*****
假设html中的代码为:
<body>
<div style="-webkit-text-size-adjust:150%">
<h2>Title</h2>
The page title is displayed in the navigation bar automatically.
<p>
<img src="moonlight.jpg" alt="picture" title="moonlight" />
<p>
<a href="javascript:clickLink();">More Information</>
<script>
function sendCommand(cmd,param){
var url="testapp:"+cmd+":"+param;
document.location = url;
}
function clickLink(){
sendCommand("alert","Muhahahaha");
}
</script>
</body>
iphone中的代码为:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestString = [[request URL] absoluteString];
NSArray *components = [requestString componentsSeparatedByString:@":"];
if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"testapp"]) {
if([(NSString *)[components objectAtIndex:1] isEqualToString:@"alert"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert from Cocoa Touch message:[components objectAtIndex:2] delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
}
return NO;
}
return YES;
}