ios wkwebview

介绍

WWDC2014推出ios8,其中webkit对应的WKWebView相对于uiwebview更加强大,内存、加载速度更优。

  • 在性能、稳定性、功能方面有很大提升
  • 允许JavaScript的Nitro库加载并使用(UIWebView中限制)
  • 支持了更多的HTML5特性
  • 高达60fps的滚动刷新率以及内置手势
  • 将UIWebViewDelegate与UIWebView重构成了14类与3个协议

环境

Mac OS X 10.10.1

Xcode 6.1.1

iOS 8.1

初始化

#import 

// 默认初始化
- (instancetype)initWithFrame:(CGRect)frame;

// 根据对webview的相关配置,进行初始化
- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration NS_DESIGNATED_INITIALIZER;

//加载网页
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
[self.view addSubview:webView];

WKWebView的代理方法

WKWebView的代理方法

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;

//页面跳转
// 接收到服务器跳转请求之后调用
- (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;

执行顺序:

webView:decidePolicyForNavigationAction:decisionHandler(如果拒绝则后续无其他回调)
webView:didStartProvisionalNavigation
webView:decidePolicyForNavigationResponse:decisionHandler(如果拒绝则收到error回调)
webView:didCommitNavigation
webView:didFinishNavigation

2. WKUIDelegate

// 创建一个新的WebView
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;

//警告框、确认框、输入框
/**
 *  web界面中有弹出警告框时调用
 *
 *  @param webView           实现该代理的webview
 *  @param message           警告框中的内容
 *  @param frame             主窗口
 *  @param completionHandler 警告框消失调用
 */
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(void (^)())completionHandler;

3. WKScriptMessageHandler

提高App与web端交互, 将接收到的JS脚本转为OC或Swift对象

// 从web界面中接收到一个脚本时调用
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message;

4.WKWebView加载JS

// 图片缩放的js代码
NSString *js = @"var count = document.images.length;for (var i = 0; i < count; i++) {var image = document.images[i];image.style.width=320;};window.alert('找到' + count + '张图');"; 

// 根据JS字符串初始化WKUserScript对象 
WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; 
// 根据生成的WKUserScript对象,初始化WKWebViewConfiguration 
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; 
[config.userContentController addUserScript:script]; 
_webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config]; 
[_webView loadHTMLString:@"![](http://upload-images.jianshu.io/upload_images/832398-a96d086fbf5dc89c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)" baseURL:nil];
[self.view addSubview:_webView];

你可能感兴趣的:(ios wkwebview)