简体   繁体   English

没有WKScriptMessage的WKWebView回调

[英]WKWebView callback without WKScriptMessage

There does not seem to be much information out there on using WKWebView with objective-c on macOS so I have been trying to make due using various iOS/Swift examples. 在macOS上使用WKWebView和Objective-c似乎没有太多信息,因此我一直在尝试使用各种iOS / Swift示例进行制作。 The problem is that my callback from JS to ObjC does not work. 问题是我从JS到ObjC的回调不起作用。 Even the breakpoint set at the userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message function is never hit. 即使在userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message函数上设置的断点也永远不会命中。

Clearly I am missing a step here. 显然,我在这里缺少一步。

My AppDelegate.m: 我的AppDelegate.m:

#import <WebKit/WebKit.h>
#import "AppDelegate.h"

@interface AppDelegate () <WKScriptMessageHandler>

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)userContentController:(WKUserContentController *)userContentController
      didReceiveScriptMessage:(WKScriptMessage *)message {
    NSLog(@"callback");
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    WKWebViewConfiguration *config = [WKWebViewConfiguration alloc];
    [[config userContentController] addScriptMessageHandler:self name: @"log"];
    WKWebView *webview = [[WKWebView alloc] initWithFrame:[[_window contentView] frame] configuration:config];
    NSString *htmlStr = @"<!DOCTYPE html><html><body><button type=\"button\" onclick=\"myFunction()\">Click Me!</button><script     type=\"text/javascript\">function myFunction() { window.webkit.messageHandlers.log.postMessage();}</script></body></html>";
    [[_window contentView] addSubview:webview];
    [webview loadHTMLString:htmlStr baseURL:nil];
} 

The reason the original code does not work is twofold. 原始代码不起作用的原因是双重的。 First is simply because WKWebviewConfiguration needs to be initialized by itself (WKWebView does not initialize its configuration for you) and second is that postMessage must have at least one argument. 首先是因为WKWebviewConfiguration需要自行初始化(WKWebView不会为您初始化其配置),其次是postMessage必须至少具有一个参数。

#import <WebKit/WebKit.h>
#import "AppDelegate.h"

@interface AppDelegate () <WKScriptMessageHandler>

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)userContentController:(WKUserContentController *)userContentController
      didReceiveScriptMessage:(WKScriptMessage *)message {
    NSLog(@"callback");
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    [[config userContentController] addScriptMessageHandler:self name: @"log"];
    WKWebView *webview = [[WKWebView alloc] initWithFrame:[[_window contentView] frame] configuration:config];
    NSString *htmlStr = @"<!DOCTYPE html><html><body><button type=\"button\" onclick=\"myFunction()\">Click Me!</button><script type=\"text/javascript\">function myFunction() { window.webkit.messageHandlers.log.postMessage(null); }</script></body></html>";
    [[_window contentView] addSubview:webview];
    [webview loadHTMLString:htmlStr baseURL:nil];
} 

Here is how you would pass an argument 这是您传递参数的方式

 - (void)userContentController:(WKUserContentController *)userContentController
       didReceiveScriptMessage:(WKScriptMessage *)message {
     if ([message.name isEqualToString:@"log"])
         NSLog(@"%@", message.body);

 }
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    [[config userContentController] addScriptMessageHandler:self name: @"log"];
    WKWebView *webview = [[WKWebView alloc] initWithFrame:[[_window contentView] frame] configuration:config];
    NSString *htmlStr = @"<!DOCTYPE html><html><body><button type=\"button\" onclick=\"myFunction()\">Click Me!</button><script type=\"text/javascript\">function myFunction() { window.webkit.messageHandlers.log.postMessage('clicked'); }</script></body></html>";
    [[_window contentView] addSubview:webview];
    [webview loadHTMLString:htmlStr baseURL:nil];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM