Web Popup urlListener works intermittently

Hi all,

I have been developing a Corona App for about a month for a client that needs to replicate an iOS app for the android market.

I’ve got a section of that app where I need to provide a quiz-like functionality and the preferred method for them to deliver the quiz questions is HTML.

I would expect a simple javascript ‘document.location = “2.html”’ to fire the urlListener, and it does… but very rarely, and the faster the phone, the more rarely it works. I’ve tested this on a HTC Desire Z and a Samsung Galaxy SII and I get the same result, sometimes my urlListener method is called and sometimes it just tries to load “corona:answeredCorrectly” as a webpage and, unsurprisingly, can’t find it.

I would appreciate any comments, questions, workarounds, advice etc.

Take a look at the code:
Main.lua

--[[  
Permission is hereby granted, free of charge, to any person obtaining a copy  
of this software and associated documentation files (the "Software"), to deal  
in the Software without restriction, including without limitation the rights  
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
copies of the Software, and to permit persons to whom the Software is  
furnished to do so, subject to the following conditions:  
  
The above copyright notice and this permission notice shall be included in  
all copies or substantial portions of the Software.  
  
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
THE SOFTWARE.  
--]]  
require("buttonEventHandler")  
  
local quizScreen = {}  
  
function quizScreen:newQuizScreen()  
  
--------------  
  
 keyHandler = {}  
keyHandler.back = function() end   
  
function onKeyEvent(event)  
 local phase = event.phase  
 local keyName = event.keyName  
 if keyName == "back" then  
 keyHandler.back(event)  
 end  
 return true  
end  
   
Runtime:addEventListener( "key", onKeyEvent );  
  
 local quizScreenDisplayGroup = display.newGroup()  
  
 local backgroundGradient = graphics.newGradient(   
{ 1, 20, 73 },   
{ 34, 123, 253 },   
"down" )  
  
 local backgroundRect = display.newRect(0,0,display.contentWidth,display.contentHeight)  
  
 backgroundRect:setFillColor(backgroundGradient)  
  
 quizScreenDisplayGroup:insert(backgroundRect)  
  
  
 local navBar = display.newGroup()  
 quizScreenDisplayGroup:insert(navBar)  
  
 local navBarGraphic = display.newImage("navBar.png", 0, 0, false)  
 navBar:insert(navBarGraphic)  
 navBarGraphic.width = display.contentWidth  
 navBarGraphic.x = navBarGraphic.width/2  
 navBarGraphic.y = navBarGraphic.height/2  
  
 local nextQuestionButtonGraphic = display.newImage("nextQuestion.png", 0, 0, false)  
 local nextQuestionButton = display.newGroup()  
  
 function reloadWebPopUp()  
 nextQuestionButton.isVisible = false  
 native.cancelWebPopup()  
 showQuizQuestionInPopUp("1.html")  
 end  
  
 local backButtonGraphic = display.newImage("reload.png", 0, 0, false)  
 local backButton = display.newGroup()  
 backButtonGraphic.x = backButtonGraphic.width/2 + (navBarGraphic.height - backButtonGraphic.height)/2  
 backButtonGraphic.y = navBarGraphic.height/2 + 1  
 backButton:insert(backButtonGraphic)  
 backButton.\_onRelease = reloadWebPopUp  
 backButton.touch = buttonEventHandler  
 backButton:addEventListener( "touch", backButton )  
  
  
 onQuizNextQuestionButtonReleased = function()  
 nextQuestionButton.isVisible = false  
 native.cancelWebPopup()  
 showQuizQuestionInPopUp("2.html")  
 end  
  
 nextQuestionButtonGraphic.x = display.contentWidth - (navBarGraphic.height/2 + nextQuestionButtonGraphic.width)/2  
 nextQuestionButtonGraphic.y = navBarGraphic.height/2 + 1  
 nextQuestionButton:insert(nextQuestionButtonGraphic)  
 nextQuestionButton.\_onRelease = onQuizNextQuestionButtonReleased  
 nextQuestionButton.touch = buttonEventHandler  
 nextQuestionButton:addEventListener( "touch", nextQuestionButton )  
 nextQuestionButton.isVisible = false  
  
 function webPopUpListener(event)  
 local shouldLoad = true  
 if string.find(event.url, "corona://questionAnsweredCorrectly") == 1 then  
 print("Add 1 to score")  
 print("Show next page button")  
 nextQuestionButton.isVisible = true  
 elseif string.find(event.url, "corona://questionAnsweredIncorrectly") == 1 then  
 print("Show next page button")  
 nextQuestionButton.isVisible = true  
 end  
 return shouldLoad  
 end  
  
 function showQuizQuestionInPopUp(quizQuestion)  
 local options = {urlRequest=webPopUpListener, hasBackground=true, baseUrl=system.ResourceDirectory, autoCancel=false }  
 native.showWebPopup(0, navBarGraphic.height, display.contentWidth, display.contentHeight-navBarGraphic.height, quizQuestion, options )  
 end  
  
  
 showQuizQuestionInPopUp("1.html")  
  
 return quizScreenDisplayGroup  
end  
  
quizScreen:newQuizScreen()  

Both 1.html and 2.html

  
[html]  
  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
<meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,user-scalable=no">  
<style type="text/css"></style>  
 \* {  
 -webkit-user-select: none;  
 -webkit-touch-callout: none;  
 -webkit-text-size-adjust: none;  
}  
  
.answer.answeredCorrectly {  
 border-color: #aae655;  
 background: url("Quiz-Correct-Tick.png") no-repeat 96% 50%, -webkit-gradient(linear, 0% 0%, 0% 100%, from(#e4f6b4), to(#aae655));  
}  
.answer.answeredIncorrectly {  
 border-color: #f5af8a;  
 background: url("Quiz-Incorrect-Cross.png") no-repeat 96% 50%, -webkit-gradient(linear, 0% 0%, 0% 100%, from(#fce5cc), to(#f5af8a));  
}  
  
  
<script type="text/javascript"><br>var questionHasBeenAnswered = false;<br><br>var answeredCorrectly = function(currentItem)<br>{	<br>	if(!questionHasBeenAnswered)<br>	{<br> document.getElementById(currentItem).setAttribute("class", "answer correct answeredCorrectly");<br> questionHasBeenAnswered = true;<br> document.location = "corona://questionAnsweredCorrectly";<br>	}<br>} <br><br>var answeredIncorrectly = function(currentItem)<br>{	<br>	if(!questionHasBeenAnswered)<br>	{<br> document.getElementById(currentItem).setAttribute("class", "answer correct answeredIncorrectly");<br> questionHasBeenAnswered = true;<br> document.location = "corona://questionAnsweredIncorrectly";<br>	}<br>} <br> </script>

  

Choose "c"

  
  

  

  
1.   

a.

  
  
2.   

b.

  
  
  
3.   

c.

  
  
4.   

d.

  
  
  

  
  
  
[/html]  

buttonEventHandler

--[[  
Permission is hereby granted, free of charge, to any person obtaining a copy  
of this software and associated documentation files (the "Software"), to deal  
in the Software without restriction, including without limitation the rights  
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
copies of the Software, and to permit persons to whom the Software is  
furnished to do so, subject to the following conditions:  
  
The above copyright notice and this permission notice shall be included in  
all copies or substantial portions of the Software.  
  
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
THE SOFTWARE.  
--]]  
function buttonEventHandler( self, event )  
 local result = true  
  
 local default = self[1]  
 local over = self[2]  
  
 -- General "onEvent" function overrides onPress and onRelease, if present  
 local onEvent = self.\_onEvent  
  
 local onPress = self.\_onPress  
 local onRelease = self.\_onRelease  
 local buttonEvent = {}  
 if (self.\_id) then  
 buttonEvent.id = self.\_id  
 end  
  
 local phase = event.phase  
 if "began" == phase then  
 if over then   
 default.isVisible = false  
 over.isVisible = true  
 end  
  
 if onEvent then  
 buttonEvent.phase = "press"  
 result = onEvent( buttonEvent )  
 elseif onPress then  
 result = onPress( event )  
 end  
  
 -- Subsequent touch events will target button even if they are outside the contentBounds of button  
 display.getCurrentStage():setFocus( self, event.id )  
 self.isFocus = true  
  
 elseif self.isFocus then  
 local bounds = self.contentBounds  
 local x,y = event.x,event.y  
 local isWithinBounds =   
 bounds.xMin \<= x and bounds.xMax \>= x and bounds.yMin \<= y and bounds.yMax \>= y  
  
 if "moved" == phase then  
 if over then  
 -- The rollover image should only be visible while the finger is within button's contentBounds  
 default.isVisible = not isWithinBounds  
 over.isVisible = isWithinBounds  
 end  
  
 elseif "ended" == phase or "cancelled" == phase then   
 if over then   
 default.isVisible = true  
 over.isVisible = false  
 end  
  
 if "ended" == phase then  
  
 -- Only consider this a "click" if the user lifts their finger inside button's contentBounds  
 if isWithinBounds then  
 if onEvent then  
 buttonEvent.phase = "release"  
 result = onEvent( buttonEvent )  
 elseif onRelease then  
 result = onRelease( event )  
 end  
 end  
 end  
  
 -- Allow touch events to be sent normally to the objects they "hit"  
 display.getCurrentStage():setFocus( self, nil )  
 self.isFocus = false  
 end  
 end  
  
 return result  
end  

[import]uid: 96471 topic_id: 19387 reply_id: 319387[/import]

Hey
Try to make sure your function that opens the webpopup always returns True as a value. Sometime I find it strange but it seem to solve a lot of stuff that happens rarely on in random order in some strange way and then I make sure all functions returns True and voila.

Good luck! [import]uid: 22737 topic_id: 19387 reply_id: 74858[/import]

I am afraid that didn’t help at all.

This problem isn’t limited to using document.location either, I’ve been doing more testing and I can’t get android to successfully use the urlListener.

Does anyone have a working example of the urlListener that builds for android? I really need some help on this and the staff aren’t responding.

Cheers [import]uid: 96471 topic_id: 19387 reply_id: 75191[/import]

Here’s a related thread:

http://developer.anscamobile.com/forum/2011/08/04/web-popup-url-handler-missing-events

Oh wait, looks like you’re already talking to Josh. Okay, nevermind. [import]uid: 26 topic_id: 19387 reply_id: 75248[/import]