问题描述
我正在尝试使用WkWebKit在应用和页面之间来回交谈。我可以使用WkWebView evaluateJavascript方法让javaScript执行得很好,但是当我尝试在JavaScript页面上执行window.webkit.messageHandlers.myHandler.postMessage('hello world!')时,我发现window.webkit没有定义。 / p>
奇怪......我正在ios 8.4的iPad模拟器中运行。我认为这可以在原版本8中找到,不是吗?
我找不到其他任何关于此的帖子,所以也许我做错了什么?
我甚至将我的Safari Developer连接到模拟器的浏览器,在控制台中我试着看看window.webkit是什么,当然,它不存在。
请注意,我在页面加载时添加了一个初始脚本(我在javascript编辑器中看到了这一点 - 记录了消息)。我还添加了一个脚本消息处理程序...
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
//加载视图后进行任何其他设置,通常是从笔尖。
NSLog(@主视图Controller viewDidLoad called ...);
//如果我们在OLD ios(v8之前)上运行,WKWebView将不存在。所以不要创建它,如果它不存在...
if(NSClassFromString(@WKWebView)){
// WKWebView无法拖到故事板上,所以必须在这里手动创建它。
//我们有一个名为_webContainer的ContainerView,这个浏览器视图将存在于
//中首先我们创建一个Web视图配置对象...
WKWebViewConfiguration * wbConfig = [WKWebViewConfiguration alloc] ;
wbConfig.mediaPlaybackAllowsAirPlay = true;
wbConfig.mediaPlaybackRequiresUserAction = false;
//在它加载之前将一些Javascript注入我们的页面...
NSString * scriptSource = @console.log('来自主持此页面的iOS应用程序......');窗口。 hostedByWkWebView = TRUE;;
WKUserScript * userScript = [[WKUserScript alloc]
initWithSource:scriptSource
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:YES];
[wbConfig.userContentController addUserScript:userScript];
[wbConfig.userContentController addScriptMessageHandler:self name:@myHandler]; // javascript使用它将是:window.webkit.messageHandlers.myHandler.postMessage
//好的,配置已创建,现在创建WkWebView实例,传入此配置。 ..
_webView = [[WKWebView alloc] initWithFrame:[_webContainer bounds] configuration:wbConfig];
_webView.autoresizesSubviews = true;
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_webView.navigationDelegate = self;
//将Web视图添加到容器视图,并告诉容器自动调整其子视图,高度和宽度。
[_webContainer addSubview:_webView];
_webContainer.autoresizesSubviews = true;
_webContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//设置webview的URL并在那里导航...
NSString * fullURL = @https://myurlgoeshere.com;
NSURL * url = [NSURL URLWithString:fullURL];
NSURLRequest * requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
}
else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@无法创建Web View消息:@使用的Web视图需要iOS 8或更高版本。抱歉。 delegate:self cancelButtonTitle:@OKotherButtonTitles:@,nil];
[alert show];
}
// ...
window.webkit命名空间仅出现在带有脚本消息处理程序的webview中。
确保您已拨打方法。
I'm experimenting with WkWebKit talking back and forth between app and page. I can get javaScript to execute fine using WkWebView evaluateJavascript method, but when I try to execute window.webkit.messageHandlers.myHandler.postMessage('hello world!') on the JavaScript page, I find that window.webkit is not defined.
Odd... I'm running in a simulator of iPad with ios 8.4. I thought this was available in original version 8, no?
I can't find anyone else posting about this, so perhaps I've done something wrong?
I've even attached my Safari Developer to the simulator's browser, and in the console I try to see what window.webkit is, and sure enough, it does not exist.
Note that I add an initial script to run when the page loads (I see this in the javascript editor - the message is logged). And I add a script message handler as well...
[EDIT: Adding more code details here]Here is my code:
- (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];
}
//...
The window.webkit namespace only appears in webview with script message handlers.Make sure that you have called addScriptMessageHandler method of WKUserContentController.
这篇关于WkWebKit - 加载页面上的javascript发现window.webkit未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!