首页 > 代码库 > iOS经验3: WebView的应用
iOS经验3: WebView的应用
镔哥认为UIWebView 控件在很多项目都要用到,毕竟我们要跟服务器打交道,很多都要请求网络,但是有些已经做好的网页,或者功能我们无法直接做,所以
IOS系统提供了 UIWebView 控件,从而可以方便地集成与网页相关的多种应用,该控件内置浏览器控件,可以用来浏览网页、打开文档等等。
一、UIWebView 可以加载和显示某个URL的网页,也可以显示基于HTML的本地网页或部分网页:
A. 加载 URL
- WebView
= [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 320, 400)]; - NSString
*path "http://www.baidu.com";= @ - NSURL
*url = [NSURL URLWithString:path]; - [WebView
loadRequest:[NSURLRequest requestWithURL:url]];
b. 加载 HTML
NSString *resPath = [bundle resourcePath];
NSString *filePath = [resPath stringByAppendingPathCom
[webView loadHTMLString:[NSString stringWithContentsOfFile
二、使用网页加载指示,加载完成后再显示网页出来
- -
(void) webViewDidStartLoad:(UIWebView *)webView - {
-
-
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; -
[view setTag:108]; -
[view setBackgroundColor:[UIColor blackColor]]; -
[view setAlpha:0.5]; -
[self.view addSubview:view]; -
-
activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)]; -
[activityIndicator setCenter:view.center]; -
[activityIndicator setActivityIndicatorView Style:UIActivityIndicatorViewS tyleWhite]; -
[view addSubview:activityIndicator]; -
-
[activityIndicator startAnimating]; - }
- -
(void) webViewDidFinishLoad:(UIWebView *)webView - {
-
[activityIndicator stopAnimating]; -
UIView *view = (UIView*)[self.view viewWithTag:108]; -
[view removeFromSuperview]; -
NSLog(@ - }
三、UIWebView还可以通过 stringByEvaluatingJavaSc
NSString *theTitle=[webView stringByEvaluatingJavaSc
NSString *currentURL = [webView stringByEvaluatingJavaSc
NSString *js_result = [webView stringByEvaluatingJavaSc
四、综合运用以上方法,还可以实现部分网页的显示,基本思路如下:
iOS经验3: WebView的应用