A simple way to get native alerts with Adobe AIR

In an AIR project I am currently working on I wanted to have native alert boxes in my app rather than building custom pop up boxes. There is no built in way to get native alerts in AIR and I remembered reading this interview over at leebrimelow.com a while back where a developer at Illume used StageWebView to trigger an alert from a HTML page. So I decided to make a class called NativeAlert that uses AIR’s HTMLLoader to trigger alerts.

The class supports all three JavaScript pop ups – Alert, Confirm and Prompt. Here is how to use each popup:

[cc lang=”actionscript”]
// Create the NativeAlert object
var nativeAlert:NativeAlert = new NativeAlert();

/* basic alert
*
* alert(message:String):void
*
*/
nativeAlert.alert(“Hello there!”);

/* confirm box
*
* confirm(message:String):Boolean
*
*/
if (nativeAlert.confirm(“Would you like to proceed?”))
{
//do domething
}
else
{
// the user cancelled
}

/* prompt box
*
* promt(message:String,defaultValue:String):String
*
*/
var userName:String = nativeAlert.promt(“please enter your name:”, “”);
[/cc]

And here is the class – copy the code into a new ActionScript file called NativeAlert.

[cc lang=”actionscript”]
package
{
import flash.html.HTMLLoader;

/**
* …
* @author Ben Foster
* -web: www.nemenvisual.com
* -blog: www.purplesquirrels.com.au
*/
public class NativeAlert
{
private var _alertDispatcher:HTMLLoader;
private var _html:String =”” +
“;

public function NativeAlert()
{
_alertDispatcher = new HTMLLoader();
_alertDispatcher.loadString(_html);
}

// invokes an alert box
public function alert(message:String):void
{
_alertDispatcher.window.alert(message);
}

// invokes a confirm box
public function confirm(message:String):Boolean
{
return _alertDispatcher.window.confirm(message);
}

// invokes a prompt box
public function prompt(message:String,defaultVal:String=””):String
{
return _alertDispatcher.window.prompt(message, defaultVal);
}

}

}
[/cc]


Comments

13 responses to “A simple way to get native alerts with Adobe AIR”

  1. really usefull thx !

  2. javierenap Avatar
    javierenap

    Muchas gracias, es muy buen aporte.
    Saludos desde México!!

  3. Great Class! Thanks for sharing! I was wondering though, is there any way to alter the window sizes (especially the prompt window) ? I am doing a prompt for a two digit number and it seems ridiculous having this huge text field (and window) to display just that. Also is there any way to change the title? If not can you or anyone else recommend a class? I have already tried out the com.adobe.air.alert classes but there are no Prompt functions. Thanks again.

    1. I am not aware of any way to alter the prompt window size. I know you cannot change the title. I believe it is a restriction for security reasons. I you want to have a user input prompt then it would probably be best to make your own prompt box. I only use this class for alerts and confirm messages.

      You could look into using a native extension (ANE). I haven’t tried it but this one supports Windows, iOS and Android:

      http://extensionsforair.com/extensions/nativealert/

      1. Richard Avatar
        Richard

        Thanks Ben, I was actually shocked about how quickly you responded to my query… gratitude. I took your advice by the way and built my own little pop up which I later reused for a password prompt so I ended up saving time in the long run!

  4. […] post on creating native alerts with Adobe AIR has proven to be quite popuplar so I decided to follow it up with something even better (at least […]

  5. This is absolutely brilliant!
    Thanks for sharing

  6. This is really cool, thanks!

  7. individual11 Avatar
    individual11

    Great workaround for something Adobe really should have made. Thanks for sharing it!

  8. I think you meant ‘prompt’, not ‘promt’ in your example of how to use it.

  9. This does NOT work on Mobile. Desktop only!

Leave a Reply