Heya,
I am using the webPopup to collect some details from the user. The process is that the webpop up asks for input and uses the urlListener to close itself, and perhaps a little inelegantly, I use a native alert to echo the values back to the user. If the user’s made a mistake, the alert opens the web pop up again.
Except that while the urlListener returning false does close the webPopup or at least hides it, subsequent calls to native.showWebPopup just silently fail. Instead the developer needs to insert a call to native.cancelWebPopup first to ensure that the webPopup appears a 2nd time.
Here’s the code I’m using which seems safe for multiple calls:
local showWebPopup
local function showNativeAlert()
-- Handler that gets notified when the alert closes
local function onComplete( event )
if "clicked" == event.action then
local i = event.index
if 1 == i then
showWebPopup()
end
end
end
-- Show alert with five buttons
local alert = native.showAlert( "See webPopUp?", "Are you sure?",
{ "Yes", "No" }, onComplete )
end
showWebPopup=function()
-- [REQUIRED] - without this line this function isn't safe to be called more than once
native.cancelWebPopup()
local function listener( event )
local url = event.url
if 1 == string.find( url, "corona:close" ) then
-- do stuff and close
showNativeAlert()
return false
end
if event.errorCode then
-- Error loading page
print( "Error: " .. tostring( event.errorMessage ))
end
return true
end
local options = { hasBackground=false, baseUrl=system.ResourceDirectory, urlRequest=listener }
native.showWebPopup( 50,50+display.statusBarHeight,display.contentWidth-100,display.contentHeight\*0.45, "input.html", options )
end
showWebPopup()
Interestingly, putting the native.cancelWebPopup() before returning false (for example line 27) in the listener causes the simulator to crash with the following console output:
objc[24868]: FREED(id): message close sent to freed object=0x514de0
/Applications/CoronaSDK/Corona Terminal: line 9: 24868 Illegal instruction: 4 "$path/Corona Simulator.app/Contents/MacOS/Corona Simulator" $\*
hsbpc12:~ tap32$ /Applications/CoronaSDK/Corona\ Terminal
[import]uid: 11757 topic_id: 19062 reply_id: 319062[/import]