1、建立UIWebViewjava
- CGRect bouds = [[UIScreen manScreen]applicationFrame];
- UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];
2、設置屬性web
- webView.scalespageToFit = YES;
- webView.detectsPhoneNumbers = YES;
3、顯示網頁視圖UIWebView瀏覽器
- [self.view addSubview:webView];
4、加載內容app
- NSURL* url = [NSURL URLWithString:@"http://www.youku.com"];//建立URL
- NSURLRequest* request = [NSURLRequest requestWithURL:url];
- [webView loadRequest:request];
也能夠加載一個本地資源:oop
- NSURL* url = [NSURL fileURLWithPath:filePath];
- NSURLRequest* request = [NSURLRequest requestWithURL:url];
- [webView loadRequest:request];
UIWebView 還支持將一個NSString對象做爲源來加載。你能夠爲其提供一個基礎URL,來指導UIWebView對象如何跟隨連接和加載遠程資源:url
- [webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];
5、導航spa
UIWebView類內部會管理瀏覽器的導航動做,經過goForward和goBack方法你能夠控制前進與後退動做:.net
- [webView goBack];
- [webView goForward];
- [webView reload];
- [webView stopLoading];
6、UIWebViewDelegate委託代理代理
UIWebView支持一組委託方法,這些方法將在特定時間獲得通知。要使用這些方法,必須先設定webView的委託:指針
下面每一個委託方法的第一個參數都是指向一個UIwebview的指針,所以你能夠將一個委託用於多個網頁視圖。
- -(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*) reuqest navigationType:(UIWebViewNavigationType)navigationType;
- UIWebViewNavigationTypeLinkClicked
- UIWebViewNavigationTypeFormSubmitted
- UIWebViewNavigationTypeBackForward
- UIWebViewNavigationTypeReload
- UIWebViewNavigationTypeFormResubmitted
- UIWebViewNavigationTypeOther
- -(void)webViewDidStartLoad:(UIWebView*)webView ;
- -(void)webViewDidFinishLoad:(UIWebView*)webView ;
- -(void)webView:(UIWebView*)webView DidFailLoadWithError:(NSError*)error;