#import "ViewController.h" #import <WebKit/WebKit.h> #import <Masonry/Masonry.h> @interface ViewController ()<WKUIDelegate,WKNavigationDelegate> @property (strong ,nonatomic) WKWebView *webView; @end @implementation ViewController #pragma mark - lifeCircle - (void)viewDidLoad { [super viewDidLoad]; self.webView = [[WKWebView alloc] init]; [self.view addSubview:self.webView]; [self.webView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view); make.right.equalTo(self.view); make.top.equalTo(self.view); make.bottom.equalTo(self.view); }]; self.webView.UIDelegate = self; self.webView.navigationDelegate = self; [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]]; } #pragma mark - WKNavigationDelegate // called when the page starts loading - (void)webView:(WKWebView *)webView DidStartProvisionalNavigation: (WKNavigation *) navigation {} / / when content began to return to the call - (void) webView: (webView WKWebView *) // call - (void)webView:(WKWebView *)webView after the page is loaded DidFinishNavigation (WKNavigation *) Navigation {} // Called when the page load failed - (void)webView (WKWebView *)webView DidFailProvisionalNavigation: (WKNavigation *) navigation {} / / receiving to the server after the jump request call - (void) webView: (webView WKWebView *) DidReceiveServerRedirectForProvisionalNavigation: (WKNavigation *) navigation {NSLog (@ "receives the server after the jump request call"); } // After receiving the response, Deciding whether to jump - (void) webView: (WKWebView *) webView decidePolicyForNavigationResponse: (navigationResponse WKNavigationResponse *)  decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{ NSLog(@"%@",navigationResponse.response.URL.absoluteString); / / allow jump decisionHandler (WKNavigationResponsePolicyAllow); / / is not allowed to jump / / decisionHandler (WKNavigationResponsePolicyCancel); NSLog(@" After receiving the response, decide whether to jump "); } // Before sending the request, Deciding whether to jump - (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: (navigationAction WKNavigationAction *) decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{ NSLog(@"%@",navigationAction.request.URL.absoluteString); / / allow jump decisionHandler (WKNavigationActionPolicyAllow); / / is not allowed to jump / / decisionHandler (WKNavigationActionPolicyCancel); NSLog(@" Decide whether to jump before sending a request "); } #pragma mark - WKUIDelegate creates a new WebView - (WKWebView *) WebView :(WKWebView *) WebView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{ return [[WKWebView alloc]init]; } / / input box - (void) webView: (WKWebView *) webView runJavaScriptTextInputPanelWithPrompt prompt: (nsstrings *) defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler{ completionHandler(@"http"); } / / confirmation box - (void) webView: (WKWebView *) webView runJavaScriptConfirmPanelWithMessage message: (nsstrings *) initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler{ completionHandler(YES); } / / alert box - (void) webView: (WKWebView *) webView runJavaScriptAlertPanelWithMessage message: (nsstrings *) initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{ NSLog(@"%@",message); completionHandler(); } @endCopy the code