How do I open HTML links in my device's browser, but not also in my app

I have written some of the content for my app in HTML and I display it in my app using the native.newWebview.  Within this HTML content, I have links to external web pages, for example https://google.com.  

When a user clicks on the link in my app, I’d like my HTML pages to stay open in the app and for Google to open up in the device’s web browser.  

However, what happens is that when they click on a link in my app, like Google, Google will open up in both my app and in the web browser.  

As a note, in iOS, it seems the first time the link is clicked, it opens up in just the device’s default browser and not the app, but if you return to the app and click the link again it will open up in both the app and the browser.  For Android it always opens up in both the app and in the browser.  

Here is a simplified version of my code.  

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 )

And here is my internal HTML file that has the link:

links.html

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

Try (tested)

links.html

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

main.lua

&nbsp; &nbsp; local function webListener( event ) &nbsp; &nbsp; &nbsp; &nbsp; if event.url then &nbsp; &nbsp; &nbsp; &nbsp; local pattern = 'corona://' &nbsp; &nbsp; &nbsp; &nbsp; local position = string.find( event.url, pattern ) or 0 &nbsp; &nbsp; &nbsp; &nbsp; local len = position == 0 and 0 or string.len( pattern )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local url = string.sub( event.url, position + len ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; system.openURL( url ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --print( url ) &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; end &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local htmlContent = native.newWebView( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight ) &nbsp; &nbsp; htmlContent:request( "links.html", system.ResourceDirectory ) &nbsp; &nbsp; htmlContent:addEventListener( "urlRequest", webListener )

@ldurniat - the solution works!  Thanks so much.  I really really appreciate it. Two follow up questions:

  1. When opening the app on my Android device, as the ‘links.html’ page loads, I get a dialog box that asks “Open with” and then lists the available browsers like Chrome, etc.  I obviously don’t want that.  Is there a way to avoid getting this dialog box?  Or is this a Android OS thing that you cannot override with Corona?

  2. I’m curious how you reached your solution.  Is this something you came up with or is there documentation somewhere recommending this solution?  It’s a very clever idea and I certainly would not have come up with this.  Just wondering if this was your own concoction or if this is a tried and true method.  

Thanks so much for your input.  It’s been a major help.   

  1. I’m not sure why it pop up. After I installed application several times (I had more than one candidate for solution) dialog box seems don’t appear any more :slight_smile: I use latest stable and free version of simulator (2016.2992). Maybe try latest daily build. 

  2. I came up with solution by using trial and error method. Some useful information I found on Corona forum and in samples attached with simulator. I have never use newWebView before. I want improve my skills by helping others on forum. 

Well thanks a lot.  Lucky for me your more clever than I am.   :slight_smile:

@ldurniat came up with this solution, but @ldurniat left out a conditional that was crucial for my code to work so I’m marking this as the complete solution.  But all credit goes to @ldurniat for coming up with this very clever solution.

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 ) 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 )

links.html

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

@ldurniat - I found out why I was getting the dialog box when I was opening links.html.  Basically, it was implementing system.openURL( url ) when links.html was opening.  But checking to see if len was greater than 0, I was able to utilize system.openURL( url ) on my link to Google, but keep links.html opening in the app without the device asking if I wanted to open it in the browser as well. 

Thanks so much for all your help with this.  This bug was driving me crazy.  

Personally, I prefer to frame web access within my game as I don’t feel it is a great user experience to leave.  IMHO this is a smoother UX.

Not to mention the lack of back button is a problem on iOS.

Try (tested)

links.html

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

main.lua

&nbsp; &nbsp; local function webListener( event ) &nbsp; &nbsp; &nbsp; &nbsp; if event.url then &nbsp; &nbsp; &nbsp; &nbsp; local pattern = 'corona://' &nbsp; &nbsp; &nbsp; &nbsp; local position = string.find( event.url, pattern ) or 0 &nbsp; &nbsp; &nbsp; &nbsp; local len = position == 0 and 0 or string.len( pattern )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local url = string.sub( event.url, position + len ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; system.openURL( url ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --print( url ) &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; end &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local htmlContent = native.newWebView( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight ) &nbsp; &nbsp; htmlContent:request( "links.html", system.ResourceDirectory ) &nbsp; &nbsp; htmlContent:addEventListener( "urlRequest", webListener )

@ldurniat - the solution works!  Thanks so much.  I really really appreciate it. Two follow up questions:

  1. When opening the app on my Android device, as the ‘links.html’ page loads, I get a dialog box that asks “Open with” and then lists the available browsers like Chrome, etc.  I obviously don’t want that.  Is there a way to avoid getting this dialog box?  Or is this a Android OS thing that you cannot override with Corona?

  2. I’m curious how you reached your solution.  Is this something you came up with or is there documentation somewhere recommending this solution?  It’s a very clever idea and I certainly would not have come up with this.  Just wondering if this was your own concoction or if this is a tried and true method.  

Thanks so much for your input.  It’s been a major help.   

  1. I’m not sure why it pop up. After I installed application several times (I had more than one candidate for solution) dialog box seems don’t appear any more :slight_smile: I use latest stable and free version of simulator (2016.2992). Maybe try latest daily build. 

  2. I came up with solution by using trial and error method. Some useful information I found on Corona forum and in samples attached with simulator. I have never use newWebView before. I want improve my skills by helping others on forum. 

Well thanks a lot.  Lucky for me your more clever than I am.   :slight_smile:

@ldurniat came up with this solution, but @ldurniat left out a conditional that was crucial for my code to work so I’m marking this as the complete solution.  But all credit goes to @ldurniat for coming up with this very clever solution.

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 ) 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 )

links.html

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

@ldurniat - I found out why I was getting the dialog box when I was opening links.html.  Basically, it was implementing system.openURL( url ) when links.html was opening.  But checking to see if len was greater than 0, I was able to utilize system.openURL( url ) on my link to Google, but keep links.html opening in the app without the device asking if I wanted to open it in the browser as well. 

Thanks so much for all your help with this.  This bug was driving me crazy.  

Personally, I prefer to frame web access within my game as I don’t feel it is a great user experience to leave.  IMHO this is a smoother UX.

Not to mention the lack of back button is a problem on iOS.