×
A new build of Corona SDK is now available to subscribers. Not a subscriber? Subscribe now.
CoronaSDK 2014.2376 | Released: 23 Jul 2014, 11:19am | What's New | Download Now

native.showWebPopup()

Description:

Creates a web popup that loads a local or remote web page.

Note: Web popups are only available on device builds.

Syntax:

native.showWebPopup( url [, options] )  
native.showWebPopup( x, y, width, height, url [, options] )

Example:

native.showWebPopup( 10, 10, 300, 300, 
                  "http://www.anscamobile.com", 
                  {urlRequest=listener} )

This example reads the html code from a local file in the /Documents directory.
local function listener( event )
        local shouldLoad = true
 
        local url = event.url
        if 1 == string.find( url, "corona:close" ) then
                -- Close the web popup
                shouldLoad = false
        end
 
        if event.errorCode then
                -- Error loading page
                print( "Error: " .. tostring( event.errorMessage )
                shouldLoad = false
        end
 
        return shouldLoad
end
 
local options = { hasBackground=false, baseUrl=system.DocumentsDirectory, urlRequest=listener }
native.showWebPopup( "localpage1.html", options )

Parameters:

url
URL of the local or remote web page. By default, the URL is assumed to be an absolute URL to a remote server. If x, y, width, height are not specified, the popup occupies the entire screen.

  • Note: To control scaling, make sure your popup's HTML head element contains the following: <meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>.
  • To prevent text within a web popup from being resized add the following style to your HTML document:
    <style type="text/css"> 
         html {        -webkit-text-size-adjust: none;  }
    </style>

x, y, width, height
If specified, these parameters control the position and dimensions of the popup.

options
Optional table containing additional parameters for the popup:

  • options.baseUrl determines whether the url parameter to native.showWebPopup() is interpreted as a relative or absolute url.

    • By default (nil) the url is absolute.
    • To refer to a local file instead, set the base url to one of the base directory constants, e.g. system.ResourceDirectory. Then the url parameter is relative to that base directory.
    • For remote files, you can also specify a remote base, and the url parameter will be relative to the remote base.
  • options.hasBackground
  • is a Boolean that controls whether the popup has a an opaque background or not. If not specified, the background will be opaque.

  • options.autoCancel
  • is a Boolean which sets up the popup to be automatically closed when the Android back key has been pressed when there is no more web history to navigate to. This is true by default.

  • options.urlRequest
  • designates a listener function that will intercept all urlRequest events from the web popup. This also provides a standard method for passing information back from a web page using pseudo-URLs. For example, the Interface/WebOverlay sample project displays a local HTML page that contains the following HTML:

    <form action="corona:close">
            <input type="submit"/>
    </form>      

    When the user clicks Submit in the web pop-up, the designated listener function is sent a urlRequest event object with its url property set to "corona:close", allowing the application to react appropriately.

    Note: The listener function must return true or the web popup will close. Without a return statement, false is assumed.

Returns:

Nothing.

Remarks:

Not supported in Corona Simulators.

Only one WebPopup object can be displayed at a time.

Webpopup objects cannot be moved or resized. They must be canceled and recreated if they need to be moved or resized.

Note: Native Web Popup objects, like other native objects don't work in groups and are always displayed on top of regular Display Objects (vector, images, and text).

Here is a link to working sample code showing how to use CSS in a local HTML file.
https://github.com/ansca/My-Portfolio/

Supported on operating systems and platforms for build numbers shown:
  • Mac OS X:
    --
  • Windows:
    --
  • iOS:
    Corona SDK 2.0
  • Android:
    Corona SDK 2.0

Replies

fdi777
User offline. Last seen 3 weeks 1 day ago. Offline
Joined: 13 Apr 2010

What happens when URL is invalid or bad host??

Event.isError like a http request, in the listener ,

amigoni
User offline. Last seen 1 year 25 weeks ago. Offline
Joined: 12 Aug 2010

It is not very clear by the example how the popup closes. How does Corona know that when shouldLoad = false that the popup should close?

Also I am trying to pass some data from a TEXTAREA back to Corona. is there any way to do this? If so is there any way to do this when the person taps the DONE button on top of the keyboard?

Thanks

coderebelbase
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 1 Feb 2010

There's a missing ")" here:

1
 print( "Error: " .. tostring( event.errorMessage )

vitalyx
User offline. Last seen 1 year 5 weeks ago. Offline
Joined: 11 Apr 2011

Is it bad to call showWebPopup() again without cancelling current web popup? Just trying to figure out if I need to add extra code to my app, like adding isWebPopupVisible flag and checking it before showWebPopup() call. Or will the old web popup be cancelled automatically without any side effects?

vweb20
User offline. Last seen 3 weeks 5 days ago. Offline
Joined: 19 Jan 2011

showWebPopup() positioning seems to work correctly (centered horizontally) for ios devices except iPad.
native.showWebPopup( (display.contentWidth-300)/2, 10, 300, 300, "http://www.anscamobile.com", {urlRequest=listener} )

on iPad the webpopup always shows in the top left corner. It seems to ignore display.contentWidth and just use some arbitrary value.

Any help regarding this issue is greatly appreciated.

horacebury
User offline. Last seen 8 hours 8 min ago. Offline
Joined: 17 Aug 2010

Why is only one web popup allowed at a time? I know this is not a limitation of Obj-C or Interface Builder...

horacebury
User offline. Last seen 8 hours 8 min ago. Offline
Joined: 17 Aug 2010

Is it correct that the web popup does not rotate when allowed orientation changes?

Tom
User is online Online
Staff
Joined: 13 Jul 2010

Web popups have many limitations. They do not rotate; can't be resized or move; only one is allowed ; and you can't change the URL of the displayed page. You must cancel and create a new web popup if you need to make any changes. Since they are a native object, you can't place them in groups and they will alway be on top of non-native display objects.

horacebury
User offline. Last seen 8 hours 8 min ago. Offline
Joined: 17 Aug 2010

Thanks Tom.

Ive seen a lot of activity around this for a while now. I know you guys are really hard at work, but do you know if there is a tech reason why this is or is it simply not a priority?

For my money, I'd love the web pop up simply to be able to show a full screen web page which can rotate with the device. I want to build a number of apps and this would e core to them all.

Matt

Tom
User is online Online
Staff
Joined: 13 Jul 2010

Matt,

I know web popups are really limited as they are now and I'm pushing to get them to work the way the rest of the native display objects work. Hopefully this can be done soon.

Tom

horacebury
User offline. Last seen 8 hours 8 min ago. Offline
Joined: 17 Aug 2010

Tom, thanks for the hard work. I know I and lots of other devs will be very happy when you get there. For me, at least, the big thing is orientation of the web popup. Thanks, Matt.

alberto1
User offline. Last seen 1 year 9 weeks ago. Offline
Joined: 9 Mar 2011

Hello,

I've got a problem when I try to use CSS.
I call a .php file and this is the one I applied the stylesheet.

Sometimes the listener functions returns this error: "The operation couldn't be completed. (NSURLErrorDomain error -999)

Could you tell me wwhat am I doing wrong?

Thanks in advance!

Alberot.

horacebury
User offline. Last seen 8 hours 8 min ago. Offline
Joined: 17 Aug 2010

Hi Tom,

Not wishing to pile on the pressure any more, but would you be allowed to let the world (ok, the forums) know when you might expect your web popup work to appear in, say, daily builds?

Thanks,

Matt.

Tom
User is online Online
Staff
Joined: 13 Jul 2010

Matt,

I wish I could tell you went it will be updated but all I can do is flag something as important and hope that it gets addressed. Everyone knows the web popups need an update but we are busy working on other things are the present. There are some exciting things in the works and I'm hoping with will be part of it.

-Tom

horacebury
User offline. Last seen 8 hours 8 min ago. Offline
Joined: 17 Aug 2010

Are web popups supposed to fire the event listener when javascript requests a URL or only when a regular link is used on the page?

Thanks,

Matt.

MBD
User offline. Last seen 2 days 5 hours ago. Offline
Joined: 14 Sep 2010

Any work around for "more than one webview per display"? That is a pretty harsh problem for me.

maarten.koopmans
User offline. Last seen 4 weeks 2 days ago. Offline
Joined: 29 Mar 2010

Is there a way to execute a javascript string in the context of the loaded page?

E.g to add a marker on a webpopup in android for a google map.

horacebury
User offline. Last seen 8 hours 8 min ago. Offline
Joined: 17 Aug 2010

Are you talking about executing javascript in the web overlay or the map control?

maarten.koopmans
User offline. Last seen 4 weeks 2 days ago. Offline
Joined: 29 Mar 2010

The web overlay

horacebury
User offline. Last seen 8 hours 8 min ago. Offline
Joined: 17 Aug 2010

If you want to just have javascript execute, treat it as normal.

If you want to execute javascript on a prompt from the lua code, you'll need a hidden iframe refreshing by way of a javascript interval or timer call. When the page reloads it's body onload event should fire the javascript.

While it may not help right now, I'm working on a completely web driven but native app where the entire user interface is contained within a full screen web control. Of course, all of the user interaction is performed using modern HTML5/CSS3/Javascript and local page requests. It's also producing the pages from XSL processed XML files.

I intend to produce a tutorial with working sample code hopefully sometime soon.

In the meantime, throw me more questions :)

m

kennw
User offline. Last seen 1 week 11 hours ago. Offline
Joined: 9 Jan 2011

... Any chance we could get a basic "touch-hold an image gives the option to save or use that image?" ;-) After finding in that in Safari, I had sorta hoped/expected that would just work.

Pretty please? :-)

Taruga
User offline. Last seen 8 weeks 5 days ago. Offline
Joined: 17 Oct 2010

I'm having some random problems with showwebpopup

Log ex.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
urlRequestoccurred
urlRequestoccurred
urlRequestoccurred
 
urlRequestoccurred
 
urlRequestoccurred
urlRequestoccurred
urlRequestoccurred
urlRequestoccurred
 
DVFreeThread - CFMachPortCreateWithPort hack = 0x1276f5e0, fPowerNotifyPort= 0x1276f500
 
DVFreeThread - CFMachPortCreateWithPort hack = 0x12771190, fPowerNotifyPort= 0x12771d50
 
urlRequestoccurred
urlRequestoccurred
urlRequestoccurred
urlRequestoccurred
2011-12-24 01:29:59.961 Corona Simulator[26809:2403] unlockFocus called too many time.
Assertion failed: (s->stack->next != NULL), function CGGStackRestore, file Context/CGGStack.c, line 116.

Taruga
User offline. Last seen 8 weeks 5 days ago. Offline
Joined: 17 Oct 2010

This is buggy as hell ! :( Can´t be used for a released product

Joshua Quick
User is online Online
Staff
Joined: 31 Jan 2011

Taruga,

Please click the "Report a Bug" link at the top of this web page and include a simple project that can reproduce the issue that you are seeing. This way the issue can get on our queue to look into. Thanks.

deniz
User offline. Last seen 54 min 55 sec ago. Offline
Joined: 22 Nov 2010

If you are loading a local file inside web popup, file name extension must be either ".html" or ".htm". Files with other extensions or without any extension name will not load properly. (tested with build 706)

Joshua Quick
User is online Online
Staff
Joined: 31 Jan 2011

deniz,

This is a limitation of the operating system's built-in web browser and not with Corona. Web browsers tend to care about the extension of the file that you are loading. TXT, PDF, and MP3 files are good examples of this.

dev01
User offline. Last seen 1 year 2 weeks ago. Offline
Joined: 20 Oct 2011

I have problem with native.showWebPopup() cause to load Url takes too long time.. so I try to show other object to cover it while loading Url but it's not work cause Native Web Popup objects are always displayed on top of regular Display Objects..also The native.newWebView API is only available for iOS devices so it's useless for me.. does anyone know someway to solve this issue?

Joshua Quick
User is online Online
Staff
Joined: 31 Jan 2011

dev01,

I recommend that you use the native.setActivityIndicator() function. This will show an animated activity indicator on top of your app, including the web popup. This function is supported on all platforms. Please see the following API documentation for more details.
http://developer.anscamobile.com/reference/index/nativesetactivityindicator

mediakitchen
User offline. Last seen 1 year 22 weeks ago. Offline
Joined: 27 Jul 2010

Deniz wrote

If you are loading a local file inside web popup, file name extension must be either ".html" or ".htm".

I had hoped to launch a local pdf file within my app. Does this mean this is not possible? My app is for iPad only.

Joshua Quick
User is online Online
Staff
Joined: 31 Jan 2011

Corona does not have any built in PDF support. I don't think there is any way to display it from Corona. You can try requesting the OS to display it by using the following code, but I don't know if it will work on iOS.

1
2
local pdfFilePath = system.pathForFile("myDocument.pdf", system.ResourceDirectory)
system.openURL(pdfFilePath)

The above will work on an Android device *ONLY* if a PDF reader app is installed on your Android device, such as the official Adobe Reader. Otherwise the above system.openURL() call will silently fail.

mediakitchen
User offline. Last seen 1 year 22 weeks ago. Offline
Joined: 27 Jul 2010

Thanks Joshua

I thought I had read somewhere that it was possible to open a pdf using a webview - is this not the case?

mediakitchen
User offline. Last seen 1 year 22 weeks ago. Offline
Joined: 27 Jul 2010

Just installed daily build 755 and did a quick test

1
2
local webView = native.newWebView( 0, 0, 320, 480 )
webView:request( "http://www.mysite.com/test.pdf" )

I created a build for IOS and tested on my iPad and the pdf was loaded and scrolls too. There are no controls on the pdf but you can pinch and zoom and it tells you what page you are on. So all in all pretty good!!

Tom
User is online Online
Staff
Joined: 13 Jul 2010

The comment section for this API has been closed. Please continue the discussion on the Corona forums. http://developer.anscamobile.com/forum/2012/04/02/web-popups