gpt4 book ai didi

javascript - WkWebKit - 加载页面上的 javascript 发现 window.webkit 未定义

转载 作者:可可西里 更新时间:2023-11-01 03:02:03 24 4
gpt4 key购买 nike

我正在试验 WkWebKit 在应用程序和页面之间来回对话。我可以使用 WkWebView evaluateJavascript 方法让 javaScript 正常执行,但是当我尝试在 JavaScript 页面上执行 window.webkit.messageHandlers.myHandler.postMessage('hello world!') 时,我发现 window.webkit 没有定义。

奇怪...我在 iOS 8.4 的 iPad 模拟器中运行。我以为这在原始版本 8 中可用,不是吗?

我找不到其他人发帖,所以也许我做错了什么?

我什至将我的 Safari Developer 附加到模拟器的浏览器,并在控制台中尝试查看 window.webkit 是什么,果然,它不存在。

请注意,我添加了一个初始脚本以在页面加载时运行(我在 javascript 编辑器中看到了这一点 - 消息已记录)。我还添加了一个脚本消息处理程序...

[编辑:在此处添加更多代码细节]这是我的代码:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"main view Controller viewDidLoad called...");

// if we are running on an OLD ios (pre v8) WKWebView will not exist. So don't create it if it doesn't exist...
if (NSClassFromString(@"WKWebView")) {
// WKWebView cannot be dragged onto storyboard, so have to create it manually here.
// We have a "ContainerView" called _webContainer that this browser view will live inside
// First we create a web view configuration object...
WKWebViewConfiguration *wbConfig = [WKWebViewConfiguration alloc];
wbConfig.mediaPlaybackAllowsAirPlay = true;
wbConfig.mediaPlaybackRequiresUserAction = false;
// inject some Javascript into our page before it even loads...
NSString *scriptSource = @"console.log('Hi from the iOS app hosting this page...'); window.hostedByWkWebView=true;";
WKUserScript *userScript = [[WKUserScript alloc]
initWithSource:scriptSource
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:YES];

[wbConfig.userContentController addUserScript:userScript];
[wbConfig.userContentController addScriptMessageHandler:self name:@"myHandler"]; // javascript to use this would be: window.webkit.messageHandlers.myHandler.postMessage


// Ok, the config is created, now create the WkWebView instance, passing in this config...
_webView = [[WKWebView alloc] initWithFrame: [_webContainer bounds] configuration:wbConfig];
_webView.autoresizesSubviews = true;
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
_webView.navigationDelegate = self;

// Add the web view to the container view, and tell the container to automatically resize its subviews, height and width.
[_webContainer addSubview:_webView];
_webContainer.autoresizesSubviews = true;
_webContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

// Set the URL for the webview and navigate there...
NSString *fullURL = @"https://myurlgoeshere.com";

NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[_webView loadRequest:requestObj];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot create Web View" message:@"The web view used requires iOS 8 or higher. Sorry." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"", nil];
[alert show];
}

//...

最佳答案

我解决了它,问题是如果你将 userContentController 设置为一个新对象,那么 userContentControllerWKScriptMessageHandler 将不会被注册在 Apple 的内部代码中正确:

WKUserContentController *userContentController = [WKUserContentController new];
userContentController.addScriptMessageHandler(self, name: "jockey")
userContentController.addScriptMessageHandler(self, name: "observe")
webView.configuration.userContentController = userContentController

通过使用 Apple 已经实例化的 userContentController 修复:

webView.configuration.userContentController.addScriptMessageHandler(self, name: "jockey")
webView.configuration.userContentController.addScriptMessageHandler(self, name: "observe")

关于javascript - WkWebKit - 加载页面上的 javascript 发现 window.webkit 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32771215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com