Corona Labs - 在设备的浏览器中打开来自 newWebView 的链接

Corona Labs - Open up links from newWebView in device's browser

我在 HTML 中为我的 Corona 应用程序编写了一些内容,并使用 native.newWebview 在我的应用程序中显示它。在这个 HTML 内容中,我有 link 到外部网页,例如 https://google.com

当用户点击我的应用程序中的 link 时,我希望我的 HTML 内容保留在应用程序中,并希望 Google 在设备的网络中打开浏览器。

但是,当他们在我的应用程序中单击 link 时,Google 将同时在我的应用程序和网络浏览器中打开。

请注意,在 iOS 中,似乎第一次单击 link 时,它只会在设备的默认浏览器中打开,而不是在应用程序中打开,但是如果您 return 到应用程序并再次单击 link 它将在应用程序和浏览器中打开。对于 Android,它总是在应用程序和浏览器中打开。

这是我的代码的简化版本。

main.lua

local function webListener( event )
    if event.url then
        system.openURL(event.url)
    end
end

htmlContent = native.newWebView( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight )
htmlContent:request( "links.html", system.ResourceDirectory )
htmlContent:addEventListener( "urlRequest", webListener )

这是应用程序中显示的 html 内容和 link。

links.html

<html>
<head>
</head>
<body>
    Go to <a href="https://google.com/">Google</a>.
</body>
</html>

尝试(已测试)

links.html

<html>
  <head></head>
  <body>
    Go to <a href="#corona://https://google.com/>Google</a>.
  </body>
</html>

main.lua

local function webListener( event )
    if event.url then
        local pattern = 'corona://'
        local position = string.find( event.url, pattern ) or 0
        local len = position == 0 and 0 or string.len( pattern ) 
        local url = string.sub( event.url, position + len )
        if len > 0 then -- ensures that the app doesn't try to open links.html in the default browser window
            system.openURL( url )
            --print( url )
        end
    end
end

local htmlContent = native.newWebView( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight-2*100 )
htmlContent:request( "links.html", system.ResourceDirectory )
htmlContent:addEventListener( "urlRequest", webListener )