iOS的UIWebView用法
UIWebView的使用方法
//1.創建、設置代理
UIWebView webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 320, 300)];
webView.delegate = self;
//2.加載網頁
NSURL url=[NSURL URLWithString:@"http://www.google.com.hk"];
NSURLRequest request=[[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:request];
//3.加載本地資源
NSURL url = [NSURL fileURLWithPath:filePath];
NSURLRequest request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
//4.是否與用戶交互(即用戶能不能控制webview)
[webView setUserInteractionEnabled:YES];
//5.顯示 UIWebView
[self.view addSubview:webView];
//6.導航
[webView goBack];//返回
[webView goForward];//向前
[webView reload];//重新加載數據
[webView stopLoading];//停止加載數據
//7.自動對頁面進行縮放以適應屏幕
webView.scalesPageToFit = YES;
//8.自動檢測網頁上的電話號碼,單擊可以撥打
webView.detectsPhoneNumbers = YES;
//9.UIWebView 還支持將一個NSString對象作為源來加載。你可以為其提供一個基礎URL,來指導UIWebView對象如何跟隨鏈接和加載遠程資源
[webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];
//10.UIWebView和JS交互
//(1)在Objective-C代碼中調用JS
//使用stringByEvaluatingJavaScriptFromString方法,需要等到UIWebView中的頁面加載完成之后去調用。
-(void) webViewDidFinishLoad:(UIWebView )webView{
[self.activityViewstopAnimating];
[myWebView stringByEvaluatingJavaScriptFromString:@"function test(){ alert(123123123)}"];
[myWebView stringByEvaluatingJavaScriptFromString:@"test();"];//調用
}
//(2)在JS中調用Objective-C代碼
//JS代碼:
function sendCommand(cmd,param){
var url="testapp:"+cmd+":"+param;
document.location = url;
}
function clickLink(){
sendCommand("alert","你好嗎?");
}
//Objective-C代碼:- (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 nil]; [alert show]; } return NO; } return YES; } </pre>
UIWebView的委托方法//1.web視圖指示加載內容時通知。應該返回YES開始加載。導航提供的類型參數,是指請求的來源,可以是下列任何一個:
//UIWebViewNavigationTypeLinkClicked 用戶觸擊了一個鏈接
//UIWebViewNavigationTypeFormSubmitted 用戶提交了一個表單
//UIWebViewNavigationTypeBackForward 用戶觸擊前進或返回按鈕
//UIWebViewNavigationTypeReload 用戶觸擊重新加載的按鈕
//UIWebViewNavigationTypeFormResubmitted 用戶重復提交表單
//UIWebViewNavigationTypeOther 發生其它行為
-(BOOL)webView:(UIWebView )webView shouldStartLoadWithRequest:(NSURLRequest )request navigationType:(UIWebViewNavigationType)navigationType;//2.開始加載的時候執行該方法。 - (void)webViewDidStartLoad:(UIWebView *)webView; //3.加載完成的時候執行該方法。 - (void)webViewDidFinishLoad:(UIWebView *)webView; //4.加載出錯的時候執行該方法。 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error; </pre>
本文由用戶 xmnx 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!