iOS8以後,蘋果推出了新框架Webkit,提供了替換UIWebView的組件WKWebView。

WKWebView的特性:

  • 性能高,穩定性好,佔用的內存比較小
  • 支持JS交互
  • 支持HTML5 新特性
  • 可以添加進度條(這個我選擇的還是第三方,最後介紹)
  • 支持內建手勢,
  • 高達60fps的滾動刷新率以及內置手勢
    一.創建
    1.導入Wbkit這個類庫
    2.創建個WebView的對象(基本與uiwebview一致)
    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
    [self.view addSubview:webView];

    3.代理方法:

    webView.UIDelegate = self;
    webView.navigationDelegate = self;

    pragma mark – WKNavigationDelegate

    1. WKNavigationDelegate來追蹤加載過程
    // 頁面開始加載時調用
    - (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;
    2.WKNavigtionDelegate頁面跳轉相關
    // 接收到服務器跳轉請求之後再執行
    - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation;
    // 在收到響應后,決定是否跳轉
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
          NSLog(@"%@",navigationResponse.response.URL.absoluteString);
    //允許跳轉
         decisionHandler(WKNavigationResponsePolicyAllow);
    //不允許跳轉
         //decisionHandler(WKNavigationResponsePolicyCancel);
    }
    // 在發送請求之前,決定是否跳轉
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;

    pragma mark – WKUIDelegate

    // 創建一個新的WebView
    - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{
    return [[WKWebView alloc]init];
    }
    // 輸入框
    - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler{
    completionHandler(@"http");
    }
    // 確認框
    - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler{
    completionHandler(YES);
    }
    // 警告框
    - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
    NSLog(@"%@",message);
    completionHandler();
    }

    關於介紹一些交互用法可參考:http://www.cocoachina.com/ios/20161121/18142.html


第三方的進度條 我使用的是NJKWebViewProgress

(當然如果用進度條,我還是使用的是uivebview的控件)
連接:https://github.com/ninjinkun/NJKWebViewProgress

#import "detailTeachVC.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"
@interface detailTeachVC ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
{
    NJKWebViewProgressView *_progressView;
    NJKWebViewProgress *_progressProxy;
}
@property(nonatomic,strong)UIWebView *webview;
@end

@implementation detailTeachVC

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor=[UIColor whiteColor];
    _webview=[[UIWebView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:_webview];

    _progressProxy = [[NJKWebViewProgress alloc] init];
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;

    _webview.delegate=_progressProxy;//#這個代理設置的不是self#

    CGFloat progressBarHeight = 2.f;
    CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;


    [self initData];
}
-(void)initData{

    NSString *url=[NSString stringWithFormat:@"https://www.baidu.com/"];
    [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar addSubview:_progressView];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    // Remove progress view
    // because UINavigationBar is shared with other ViewControllers
    [_progressView removeFromSuperview];
}
#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_progressView setProgress:progress animated:YES];
    self.title = [_webview stringByEvaluatingJavaScriptFromString:@"document.title"];
}
#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView
{

}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{

}
分享