An object that displays interactive web content, such as for an in-app browser.html
WKWebView属于WebKit.framework框架,Apple在WWDC 2014中随iOS 8和OS X 10.10提出来的,为了解决UIWebView加载速度慢、占用内存大的问题。 使用UIWebView加载网页的时候,内存会无限增加,还有内存泄漏的问题存在。 WebKit中更新的WKWebView控件的新特性与使用方法,很好的解决了UIWebView存在的内存、加载速度等诸多问题。支持到iOS8,相对于UIWebView,WKWebView有不少明显优点:java
UIWebViewios
WKWebViewweb
因此,使用WkWebview替换UIWebView仍是颇有必要的。macos
WebKit.framework
框架,而且引入头文件#import <WebKit/WebKit.h>
// 初始化1 - (instancetype)initWithFrame:(CGRect)frame; // 初始化2,WKWebViewConfiguration能够用来设置与js交互的相关配置 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration;
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds]; [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://developer.apple.com/reference/webkit"]]]; [self.view addSubview:webView];
//建立配置 WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; //建立UserContentController(提供JavaScript向webView发送消息的方法) WKUserContentController *userContent = [[WKUserContentController alloc] init]; //添加消息处理,self指代的对象须要遵照WKScriptMessageHandler协议,结束时须要移除 [userContent addScriptMessageHandler:self name:@"NativeMethod"]; //将UserConttentController设置到配置文件 config.userContentController = userContent; //自定义配置建立WKWebView WKWebView *webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds configuration:config]; //设置访问的URL NSURL *url = [NSURL URLWithString:@"https://developer.apple.com/reference/webkit"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [webView loadRequest:request]; [self.view addSubview:webView]; //实现WKScriptMessageHandler协议方法 - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { // 判断是不是调用原生的 if ([@"NativeMethod" isEqualToString:message.name]) { // 判断message的内容,而后作相应的操做 if ([@"close" isEqualToString:message.body]) { } } } //将当前ViewController设置为MessageHandler以后须要在当前ViewController销毁前将其移除,不然会形成内存泄漏 - (void)dealloc{ //WKUserContentController *userContentController [userContentController removeScriptMessageHandlerForName:@"NativeMethod"]; }
/*! @abstract A copy of the configuration with which the web view was initialized. */ //只读的配置属性 @property (nonatomic, readonly, copy) WKWebViewConfiguration *configuration; /*! @abstract The web view's navigation delegate. */ //导航代理 @property (nullable, nonatomic, weak) id <WKNavigationDelegate> navigationDelegate; /*! @abstract The web view's user interface delegate. */ //用户交互代理 @property (nullable, nonatomic, weak) id <WKUIDelegate> UIDelegate; /*! @abstract The web view's back-forward list. */ //前进后退列表 @property (nonatomic, readonly, strong) WKBackForwardList *backForwardList; /*! @abstract The page title. @discussion @link WKWebView @/link is key-value observing (KVO) compliant for this property. */ //网页标题,能够用kvo监听 @property (nullable, nonatomic, readonly, copy) NSString *title; /*! @abstract The active URL. @discussion This is the URL that should be reflected in the user interface. @link WKWebView @/link is key-value observing (KVO) compliant for this property. */ //请求的url,可用kvo监听 @property (nullable, nonatomic, readonly, copy) NSURL *URL; /*! @abstract A Boolean value indicating whether the view is currently loading content. @discussion @link WKWebView @/link is key-value observing (KVO) compliant for this property. */ //当前是否正在加载网页,可用kvo监听 @property (nonatomic, readonly, getter=isLoading) BOOL loading; /*! @abstract An estimate of what fraction of the current navigation has been completed. @discussion This value ranges from 0.0 to 1.0 based on the total number of bytes expected to be received, including the main document and all of its potential subresources. After a navigation completes, the value remains at 1.0 until a new navigation starts, at which point it is reset to 0.0. @link WKWebView @/link is key-value observing (KVO) compliant for this property. */ //加载进度范围0~1,可用kvo监听 @property (nonatomic, readonly) double estimatedProgress; /*! @abstract A Boolean value indicating whether all resources on the page have been loaded over securely encrypted connections. @discussion @link WKWebView @/link is key-value observing (KVO) compliant for this property. */ //标识页面中的全部资源是否经过安全加密链接来加载,可用kvo监听 @property (nonatomic, readonly) BOOL hasOnlySecureContent; /*! @abstract A Boolean value indicating whether horizontal swipe gestures will trigger back-forward list navigations. @discussion The default value is NO. */ //是否支持左右的swipe手势是否能够前进、后退 @property (nonatomic) BOOL allowsBackForwardNavigationGestures; /*! @abstract The custom user agent string or nil if no custom user agent string has been set. */ //用户自定义的user agent 没有则为nil @property (nullable, nonatomic, copy) NSString *customUserAgent API_AVAILABLE(macosx(10.11), ios(9.0)); /*! @abstract A Boolean value indicating whether link preview is allowed for any links inside this WKWebView. @discussion The default value is YES on Mac and iOS. */ //标识不容许连接预览,在iOS上默认为NO @property (nonatomic) BOOL allowsLinkPreview API_AVAILABLE(macosx(10.11), ios(9.0)); #if TARGET_OS_IPHONE /*! @abstract The scroll view associated with the web view. */ //展现网页内容的scrollview @property (nonatomic, readonly, strong) UIScrollView *scrollView; #endif #if !TARGET_OS_IPHONE /* @abstract A Boolean value indicating whether magnify gestures will change the web view's magnification. @discussion It is possible to set the magnification property even if allowsMagnification is set to NO. The default value is NO. */ //是否能够放大,默认为no @property (nonatomic) BOOL allowsMagnification; /* @abstract The factor by which the page content is currently scaled. @discussion The default value is 1.0. */ //放大因子,默认1 @property (nonatomic) CGFloat magnification;
/*! @abstract Navigates to the back item in the back-forward list. @result A new navigation to the requested item, or nil if there is no back item in the back-forward list. */ //返回上一页,若是不能返回则什么都不作 - (nullable WKNavigation *)goBack; /*! @abstract Navigates to the forward item in the back-forward list. @result A new navigation to the requested item, or nil if there is no forward item in the back-forward list. */ //前进一页 若是不能前进则什么都不作 - (nullable WKNavigation *)goForward; /*! @abstract Reloads the current page. @result A new navigation representing the reload. */ //从新加载页面 - (nullable WKNavigation *)reload; /*! @abstract Reloads the current page, performing end-to-end revalidation using cache-validating conditionals if possible. @result A new navigation representing the reload. */ //从新加载原始URL - (nullable WKNavigation *)reloadFromOrigin; /*! @abstract Stops loading all resources on the current page. */ //中止加载 - (void)stopLoading; /* @abstract Evaluates the given JavaScript string. @param javaScriptString The JavaScript string to evaluate. @param completionHandler A block to invoke when script evaluation completes or fails. @discussion The completionHandler is passed the result of the script evaluation or an error. */ //执行JS代码 - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler; /* @abstract Scales the page content by a specified factor and centers the result on a specified point. * @param magnification The factor by which to scale the content. * @param point The point (in view space) on which to center magnification. */ //根据设置的缩放因子来缩放页面,并居中显示结果在指定的点 - (void)setMagnification:(CGFloat)magnification centeredAtPoint:(CGPoint)point;
WKNavigationDelegate主要处理一些跳转、加载处理操做,因此WKNavigationDelegate 更加经常使用。若是实现了代理方法,必定要在decidePolicyForNavigationAction和decidePolicyForNavigationResponse方法中的回调设置容许跳转。安全
//1.在发送请求以前,决定是否跳转 //WKNavigationActionPolicyCancel 取消跳转 //WKNavigationActionPolicyAllow 容许跳转 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { NSLog(@"1-------在发送请求以前,决定是否跳转 -->%@",navigationAction.request); decisionHandler(WKNavigationActionPolicyAllow); } //2.页面开始加载时调用 - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation { NSLog(@"2----页面开始加载时调用"); } //3.收到响应后,决定是否跳转 - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler { //在收到服务器的响应头,根据response相关信息,决定是否跳转。decisionHandler必须调用,来决定是否跳转,参数WKNavigationActionPolicyCancel取消跳转,WKNavigationActionPolicyAllow容许跳转 NSLog(@"3-------在收到响应后,决定是否跳转"); decisionHandler(WKNavigationResponsePolicyAllow); } //4.当内容开始返回时调用 - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation { NSLog(@"4-------当内容开始返回时调用"); } //5.页面加载完成以后调用 - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { NSLog(@"5-------页面加载完成以后调用"); } //6.页面加载失败时调用 - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation { NSLog(@"6-------页面加载失败时调用"); } //接收到服务器跳转请求以后调用 - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation { NSLog(@"-------接收到服务器跳转请求以后调用"); } //数据加载发生错误时调用 - (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error { NSLog(@"----数据加载发生错误时调用"); } // 须要响应身份验证时调用 一样在block中须要传入用户身份凭证 - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler { //用户身份信息 NSLog(@"----须要响应身份验证时调用 一样在block中须要传入用户身份凭证"); NSURLCredential *newCred = [NSURLCredential credentialWithUser:@"" password:@"" persistence:NSURLCredentialPersistenceNone]; // 为 challenge 的发送方提供 credential [[challenge sender] useCredential:newCred forAuthenticationChallenge:challenge]; completionHandler(NSURLSessionAuthChallengeUseCredential,newCred); } // 进程被终止时调用 - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView { NSLog(@"----------进程被终止时调用"); }
WKUIDelegate主要处理JS脚本,确认框,警告框等。服务器
/** * web界面中有弹出警告框时调用 * @param webView 实现该代理的webview * @param message 警告框中的内容 * @param completionHandler 警告框消失调用 */ - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(void (^)())completionHandler { NSLog(@"web界面中有弹出警告框时调用"); } //建立新的webView时调用的方法 - (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures { NSLog(@"建立新的webView时调用的方法"); return webView; } //关闭webView时调用的方法 - (void)webViewDidClose:(WKWebView *)webView { NSLog(@"关闭webView时调用的方法"); } //下面这些方法是交互JavaScript的方法 //JavaScript调用confirm方法后回调的方法 confirm是js中的肯定框,须要在block中把用户选择的状况传递进去 -(void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler { NSLog(@"%@",message); completionHandler(YES); } //JavaScript调用prompt方法后回调的方法 prompt是js中的输入框 须要在block中把用户输入的信息传入 -(void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler{ NSLog(@"%@",prompt); completionHandler(@"123"); } //默认预览元素调用 - (BOOL)webView:(WKWebView *)webView shouldPreviewElement:(WKPreviewElementInfo *)elementInfo { NSLog(@"默认预览元素调用"); return YES; } //返回一个视图控制器将致使视图控制器被显示为一个预览。返回nil将WebKit的默认预览的行为。 - (nullable UIViewController *)webView:(WKWebView *)webView previewingViewControllerForElement:(WKPreviewElementInfo *)elementInfo defaultActions:(NSArray<id <WKPreviewActionItem>> *)previewActions { NSLog(@"返回一个视图控制器将致使视图控制器被显示为一个预览。返回nil将WebKit的默认预览的行为。"); return self; } //容许应用程序向它建立的视图控制器弹出 - (void)webView:(WKWebView *)webView commitPreviewingViewController:(UIViewController *)previewingViewController { NSLog(@"容许应用程序向它建立的视图控制器弹出"); } // 显示一个文件上传面板。completionhandler完成处理程序调用后打开面板已被撤销。经过选择的网址,若是用户选择肯定,不然为零。若是不实现此方法,Web视图将表现为若是用户选择了取消按钮。 - (void)webView:(WKWebView *)webView runOpenPanelWithParameters:(WKOpenPanelParameters *)parameters initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSArray<NSURL *> * _Nullable URLs))completionHandler { NSLog(@"显示一个文件上传面板"); }