UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效?

UIWebView delegate method shouldStartLoadWithRequest: equivalent in WKWebView?(UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效?)

本文介绍了UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 iOS 7+ 应用程序中有一个模块,它是 UIWebView.html 页面加载创建自定义形状按钮的 javascript(使用 Raphaeljs 库).使用 UIWebView,我将委托设置为 self.每次按下我的自定义按钮之一时,都会调用委托方法 webView: shouldStartLoadWithRequest: navigationType: .请求不应由 html 处理,而应由 iOS 代码处理.所以我使用了一个请求约定(在stackoverflow上的某个地方阅读),使用inapp"作为我的请求方案.然后我检查主机并采取适当的措施.

I have a module inside my iOS 7+ app which is a UIWebView. The html page loads a javascript that creates custom-shaped buttons (using the Raphaeljs library). With UIWebView, I set delegate to self. The delegate method webView: shouldStartLoadWithRequest: navigationType: is called each time one of my custom button is pressed. The requests should not be handled by the html, but rather by the iOS code. So I used a request convention (read somewhere here on stackoverflow) using "inapp" as the scheme of my requests. I then check for the host and take the appropriate action.

此代码在 iOS 7 上运行良好.但在 iOS 8 上 Web 视图显示为空白(错误?),因此我决定在 iOS 8 设备上使用 WKWebView.Web 视图现在渲染得很好(而且速度惊人地快!),但我的按钮没有效果.

This code works fine on iOS 7. But the web views appear blank on iOS 8 (bug?), so I decided to use WKWebView for iOS 8 devices. The web views now render fine (and amazingly faster!), but my buttons have no effect.

我尝试使用 - (WKNaviation *)loadRequest:(NSURLRequest *)request,但它没有被调用.

I tried using - (WKNaviation *)loadRequest:(NSURLRequest *)request, but it's not called.

我找不到 UIWebView 委托方法 webView: shouldStartLoadWithRequest: navigationType: 的直接等效项.使用 WKWebView 处理这些请求的最佳方式是什么?

I can't find a direct equivalent of the UIWebView delegate method webView: shouldStartLoadWithRequest: navigationType:. What's the best way of handling those requests with WKWebView?

推荐答案

重新阅读您的描述,您实际上需要了解的是如何使用 WKWebView 重新实现 Javascript/Objective-C 桥.

Re-reading your description it looks like what you actually need to know about is how to reimplement a Javascript/Objective-C bridge using WKWebView.

我自己刚刚按照 http://tetontech.wordpress.com/2014/07/17/objective-c-wkwebview-to-javascript-and-back/ 和 http://nshipster.com/wkwebkit/

I've just done this myself, following the tutorial at http://tetontech.wordpress.com/2014/07/17/objective-c-wkwebview-to-javascript-and-back/ and the info at http://nshipster.com/wkwebkit/

WKWebView 有一个内置的 Javascript 和 Objective-C/Swift 之间的通信方式:WKScriptMessageHandler.

WKWebView has a built-in way of communicating between Javascript and Objective-C/Swift: WKScriptMessageHandler.

首先,在视图控制器的标头中包含 WebKit 标头和 WKScriptMessageHandler 协议:

First, include the WebKit headers and WKScriptMessageHandler protocol in your view controller's header:

#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
@interface ViewController : UIViewController <WKScriptMessageHandler>

@end

当初始化你的WKWebView时,你需要用一个脚本消息处理程序来配置它.随意命名,但对我来说,为您的应用命名似乎是有意义的.

The when initialising your WKWebView, you need to configure it with a script message handler. Name it whatever you want, but to me it seems like naming it for your app makes sense.

    WKWebViewConfiguration *theConfiguration = 
          [[WKWebViewConfiguration alloc] init];
    [theConfiguration.userContentController 
          addScriptMessageHandler:self name:@"myApp"];

    _theWebView = [[WKWebView alloc] initWithFrame:self.view.frame 
                      configuration:theConfiguration];
    [_theWebView loadRequest:request];
    [self.view addSubview:_theWebView];

现在,实现 userContentController:didReceiveScriptMessage:.当您的 webview 收到一条消息时,它会触发,因此它会完成您之前使用 webView:shouldStartLoadWithRequest:navigationType: 所做的工作.

Now, implement userContentController:didReceiveScriptMessage:. This fires when your webview receives a message, so it does the work you were previously doing with webView:shouldStartLoadWithRequest:navigationType:.

- (void)userContentController:(WKUserContentController *)userContentController 
                        didReceiveScriptMessage:(WKScriptMessage *)message {
    NSDictionary *sentData = (NSDictionary *)message.body;
    NSString *messageString = sentData[@"message"];
    NSLog(@"Message received: %@", messageString);
}

您现在可以接收来自 Javascript 的消息了.您需要添加到 Javascript 中的函数调用如下:

You're now ready to receive messages from Javascript. The function call you need to add to your Javascript is this:

window.webkit.messageHandlers.myApp.postMessage({"message":"Hello there"});

这篇关于UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效?

基础教程推荐