想用UIWebVeiw作的,可是忽然想起來在iOS8中出了一個新的WKWebView,算是UIWebVeiw的升級版。本着對新事物的好奇,就上網查了一下,可是找了好多個都沒說的多了詳細,因而就問谷歌,找文檔,看看使用方法,試用了一下,果真不錯,記錄下來,你們分享!javascript
導入Wbkit這個類庫(WKWebVeiw包含在裏面的)java
遵照協議(通常前兩個就行啦,第三個主要是與JS相關的東西,這個協議中包含一個必須實現的方法,這個方法是提升App與web端交互的關鍵,它能夠直接將接收到的JS腳本轉爲OC或Swift對象--網上大神說的,我沒用過。)web
@interface ViewController : UIViewController<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler>
3.建立個WebView的對象服務器
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds]; [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]]; [self.view addSubview:webView];
這裏有個注意點,網址必定要寫完整,加上https://
我剛開始懶,沒有加,屋裏怎麼試都加載不出來,顯示個白屏!!血的教訓啊!!ide
WKWebView代理有兩個,是WKNavigationDelegate
和WKUIDelegate
,能夠根據須要來決定用那些方法(固然使用以前不要忘了遵照協議)。性能
他們兩個的代理方法有好多個,不過有的不經常使用就不寫了,須要的本身點進去看一下就行啦。。。
spa
1. WKNavigationDelegate來追蹤加載過程.net
// 頁面開始加載時調用 - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation; // 當內容開始返回時調用 - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation; // 頁面加載完成以後調用 - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation; // 頁面加載失敗時調用 - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation;
WKNavigtionDelegate來進行頁面跳轉代理
// 接收到服務器跳轉請求以後再執行 - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation; // 在收到響應後,決定是否跳轉 - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler; // 在發送請求以前,決定是否跳轉 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
2. WKUIDelegatecode
//1.建立一個新的WebVeiw - (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures; //2.WebVeiw關閉(9.0中的新方法) - (void)webViewDidClose:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0); //3.顯示一個JS的Alert(與JS交互) - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler; //4.彈出一個輸入框(與JS交互的) - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler; //5.顯示一個確認框(JS的) - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler;
(PS:WKUIDelegate主要是作跟網頁交互的,能夠顯示javascript的一些alert或者Action,看起來跟本身作的同樣的.)
也能夠參考這篇博客http://blog.csdn.net/baihuaxiu123/article/details/51674726