一、创建UIWebView
[java] view plain copy print ?
- CGRect bouds = [[UIScreen manScreen]applicationFrame];
- UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];
CGRect bouds = [[UIScreen manScreen]applicationFrame]; UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];二、设置属性
[java] view plain copy print ?
- webView.scalespageToFit = YES;
- webView.detectsPhoneNumbers = YES;
webView.scalespageToFit = YES;//自动对页面进行缩放以适应屏幕 webView.detectsPhoneNumbers = YES;//自动检测网页上的电话号码,单击可以拨打
三、显示网页视图UIWebView
[java] view plain copy print ?
- [self.view addSubview:webView];
[self.view addSubview:webView];四、加载内容
[java] view plain copy print ?
- NSURL* url = [NSURL URLWithString:@"http://www.youku.com"];//创建URL
- NSURLRequest* request = [NSURLRequest requestWithURL:url];
- [webView loadRequest:request];
NSURL* url = [NSURL URLWithString:@"http://www.youku.com"];//创建URL NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest [webView loadRequest:request];//加载
也可以加载一个本地资源:
[java] view plain copy print ?
- NSURL* url = [NSURL fileURLWithPath:filePath];
- NSURLRequest* request = [NSURLRequest requestWithURL:url];
- [webView loadRequest:request];
NSURL* url = [NSURL fileURLWithPath:filePath];//创建URL NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest [webView loadRequest:request];//加载
UIWebView 还支持将一个NSString对象作为源来加载。你可以为其提供一个基础URL,来指导UIWebView对象如何跟随链接和加载远程资源:
[java] view plain copy print ?
- [webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];
[webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];
五、导航
UIWebView类内部会管理浏览器的导航动作,通过goForward和goBack方法你可以控制前进与后退动作:
[java] view plain copy print ?
- [webView goBack];
- [webView goForward];
- [webView reload];
- [webView stopLoading];
[webView goBack]; [webView goForward]; [webView reload];//重载 [webView stopLoading];//取消载入内容
六、UIWebViewDelegate委托代理
UIWebView支持一组委托方法,这些方法将在特定时间得到通知。要使用这些方法,必须先设定webView的委托:
[java] view plain copy print ?
- webView.delegate = self;
webView.delegate = self;
下面每个委托方法的第一个参数都是指向一个UIwebview的指针,因此你可以将一个委托用于多个网页视图。
[java] view plain copy print ?
- -(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*) reuqest navigationType:(UIWebViewNavigationType)navigationType;
- UIWebViewNavigationTypeLinkClicked
- UIWebViewNavigationTypeFormSubmitted
- UIWebViewNavigationTypeBackForward
- UIWebViewNavigationTypeReload
- UIWebViewNavigationTypeFormResubmitted
- UIWebViewNavigationTypeOther
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*) reuqest navigationType:(UIWebViewNavigationType)navigationType;//当网页视图被指示载入内容而得到通知。应当返回YES,这样会进行加载。通过导航类型参数可以得到请求发起的原因,可以是以下任意值: UIWebViewNavigationTypeLinkClicked UIWebViewNavigationTypeFormSubmitted UIWebViewNavigationTypeBackForward UIWebViewNavigationTypeReload UIWebViewNavigationTypeFormResubmitted UIWebViewNavigationTypeOther
[java] view plain copy print ?
- -(void)webViewDidStartLoad:(UIWebView*)webView ;
-(void)webViewDidStartLoad:(UIWebView*)webView ;//当网页视图已经开始加载一个请求后,得到通知。
[java] view plain copy print ?
- -(void)webViewDidFinishLoad:(UIWebView*)webView ;
-(void)webViewDidFinishLoad:(UIWebView*)webView ;//当网页视图结束加载一个请求之后,得到通知。
[java] view plain copy print ?
- -(void)webView:(UIWebView*)webView DidFailLoadWithError:(NSError*)error;