Javascript 不带WKScriptMessage的WKWebView回调
关于在macOS上使用objective-c的Javascript 不带WKScriptMessage的WKWebView回调,javascript,objective-c,macos,webkit,wkwebview,Javascript,Objective C,Macos,Webkit,Wkwebview,关于在macOS上使用objective-c的WKWebView似乎没有太多信息,所以我一直在尝试使用各种iOS/Swift示例。问题是我从JS到ObjC的回调不起作用。即使在userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(WKScriptMessage*)message函数中设置的断点也不会被命中 很明显,我在这里错过了一步 我的AppDelegate.
WKWebView
似乎没有太多信息,所以我一直在尝试使用各种iOS/Swift示例。问题是我从JS到ObjC的回调不起作用。即使在userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(WKScriptMessage*)message
函数中设置的断点也不会被命中
很明显,我在这里错过了一步
我的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];
}
#导入
#导入“AppDelegate.h”
@接口AppDelegate()
@属性(弱)窗口*窗口;
@结束
@实现AppDelegate
-(void)userContentController:(WKUserContentController*)userContentController
didReceiveScriptMessage:(WKScriptMessage*)消息{
NSLog(@“回调”);
}
-(无效)ApplicationIDFinishLaunching:(NSNotification*)通知{
WKWebViewConfiguration*配置=[WKWebViewConfiguration alloc];
[[config userContentController]addScriptMessageHandler:self name:@“log”];
WKWebView*webview=[[WKWebView alloc]initWithFrame:[[[U window contentView]frame]配置:配置];
NSString*htmlStr=@“单击我!函数myFunction(){window.webkit.messageHandlers.log.postMessage();}”;
[[u window contentView]addSubview:webview];
[webview loadHTMLString:htmlStr baseURL:nil];
}
原始代码不起作用的原因有两个。第一个原因很简单,因为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];
}
#导入
#导入“AppDelegate.h”
@接口AppDelegate()
@属性(弱)窗口*窗口;
@结束
@实现AppDelegate
-(void)userContentController:(WKUserContentController*)userContentController
didReceiveScriptMessage:(WKScriptMessage*)消息{
NSLog(@“回调”);
}
-(无效)ApplicationIDFinishLaunching:(NSNotification*)通知{
WKWebViewConfiguration*配置=[[WKWebViewConfiguration alloc]init];
[[config userContentController]addScriptMessageHandler:self name:@“log”];
WKWebView*webview=[[WKWebView alloc]initWithFrame:[[[U window contentView]frame]配置:配置];
NSString*htmlStr=@“单击我!函数myFunction(){window.webkit.messageHandlers.log.postMessage(null);}”;
[[u window contentView]addSubview:webview];
[webview loadHTMLString:htmlStr baseURL:nil];
}
下面是你将如何传递一个论点
- (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];
}
-(void)userContentController:(WKUserContentController*)userContentController
didReceiveScriptMessage:(WKScriptMessage*)消息{
如果([message.name IsequalString:@“log”])
NSLog(@“%@”,message.body);
}
-(无效)ApplicationIDFinishLaunching:(NSNotification*)通知{
WKWebViewConfiguration*配置=[[WKWebViewConfiguration alloc]init];
[[config userContentController]addScriptMessageHandler:self name:@“log”];
WKWebView*webview=[[WKWebView alloc]initWithFrame:[[[U window contentView]frame]配置:配置];
NSString*htmlStr=@“单击我!函数myFunction(){window.webkit.messageHandlers.log.postMessage('clicked');}”;
[[u window contentView]addSubview:webview];
[webview loadHTMLString:htmlStr baseURL:nil];
}
问题解决了吗?我面临着这个问题currently@John我最终还是弄明白了。只是忘了把答案贴出来。我设法找到了其他的解决办法,但无论如何还是要感谢=)希望它能让别人受益