UIWebView委托方法shouldStartLoadWithRequest:在WKWebView中等效? [英] UIWebView delegate method shouldStartLoadWithRequest: equivalent in WKWebView?

查看:256
本文介绍了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视图显示为空白(错误?),所以我决定使用WKWebView for iOS 8设备。网页视图现在渲染得很好(而且速度惊人!),但我的按钮没有效果。

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 *)请求,但它没有被调用。

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 协议: / p>

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时,您需要使用脚本消息处理程序对其进行配置。将它命名为你想要的任何东西,但对我而言,似乎为你的app命名它是有意义的。

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中等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆