bview使用javascript在移动safari中打开URL

bview使用javascript在移动safari中打开URL

本文介绍了UIWebview使用javascript在移动safari中打开URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用移相器构建一个HTML5游戏,我有一个按钮在javascript中调用这样的URL:

So I'm building an HTML5 game using phaser and I have a button that calls a URL in javascript like this:

window.open("http://google.com", "_blank");

这在新窗口中打开桌面效果很好,我也可以打开它在Android设备上的默认浏览器中也可以通过webview访问。但在使用swift的UIWebView的iOS上我使用以下代码

This works great on desktop opening in a new window, I'm also able to get it to open in the default browser on an android device via webview as well. But on iOS using a UIWebView in swift I'm using the following code

func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
        if navigationType == UIWebViewNavigationType.LinkClicked {
            UIApplication.sharedApplication().openURL(request.URL)
            return false
        }
        return true
    }

它触发所以是的我设置了委托,我甚至在这里有一个断点所以我可以查看属性, request.URL 无处可寻。这些属性都没有我可以用来检测和调用移动游戏的URL ...

It triggers so yes I have the delegate set, I even have a breakpoint in here so I could look through the properties, and request.URL is nowhere to be found. None of these properties have a URL I can use to detect and call mobile safari...

我在网上找到的所有内容都说只知道使用标准链接标签如< a href =http://google.com>链接文字< / a> 我还尝试了 document.location。 href =http://google.com; 没有运气。顺便说一下,我用< a href =http://google.com>链接文字< / a> 进行了测试,这个功能有效,它会打开在野生动物园。当通过Javascript调用URL时,它不起作用。

Everything I've found online just says it's only known to work with standard link tags like <a href="http://google.com">link text</a> I also tried document.location.href = "http://google.com"; with no luck. Btw, I did test with <a href="http://google.com">link text</a> and this function works, it makes it open in safari. It just doesn't work when the URL is called via Javascript.

此外, webViewDidStartLoad / webViewDidFinishLoad 都触发断点当调用URL时,在webView对象中提供的任何属性中都找不到任何URL ...

Also, webViewDidStartLoad/webViewDidFinishLoad both trigger breakpoints when the URL is called yet there is no URL anywhere to be found in any of the properties provided in the webView object...

任何想法如何获得委托使用javascript调用URL可以很好地发挥作用?或者任何有关工作的想法?它只是加载webview中的链接...我只需要它在移动safari中打开这些链接。

Any ideas how I can get the delegate functions to play well with with javascript calling the URL? Or any ideas for work arounds? It's just loading the link inside the webview... I just need it to open those links in mobile safari.

推荐答案

想象它出来了!只需在你的html页面上的某个地方做一个空标记,其中包含以下内容的游戏内容:

Figured it out! Just make an empty a tag somewhere on your html page that conatins the game content like:

<a id="theLink" target="_blank" href="http://google.com"></a>

然后使用这样的Javascript点击它:

Then "click" it using Javascript like this:

document.getElementById('theLink').click();

与上面的swift代码一起使用对我有用,游戏打开safari中的链接。

That used in conjunction with the swift code above is working for me and the game opens the link in safari.

这篇关于UIWebview使用javascript在移动safari中打开URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 09:34