On iOS PhoneGap Notifications
The documentation for this in the PG docs are a little anemic when it comes to this topic for some reason, so I’m putting this out there.
Some of the time when you present a modal dialog like ``alert'' and almost always when your present a ``confirm'' you want to perform some action when the dialog is dismissed.
The ``alert()'' and ``confirm()'' calls in normal web development are blocking and thus you do not use the typical “callback” event-driven method you might be used to in the mostly event-driven world of web development. In these cases the main-thread is blocked until the dialog is dismissed and you’re passed back some sort of data on which button was pressed (in the case of a confirmation dialog).
The call for showing an alert in PhoneGap looks something like this (and this is how it is presented in the documentation):
navigator.notification.alert(message, title, buttonText);
What isn’t documented at all is that this method returns what Obj-C/Cocoa developers call a “delegate” (checked into master on May 10, 2010). A delegate is just an object which has various known method names which the Notification object can call when the dialog is dismissed. This is pretty much like a callback, except instead of just passing in a named or anonymous callback to the ``notification.alert'' call, you are returned this delegate which you can plop your callback into, like so:
delegate = navigator.notification.alert(message, title, buttonText);
delegate.onAlertDismissed = callback;
When the user clicks the button on this alert, the ``callback'' method will be executed and passed in two bits of information, an ``index'' and a ``label'' which correspond to the button you pressed.
If you looked at the docs, you might wonder why this is useful when you can only show an alert which only has a single button. Well what is also not in the docs is that there is a parallel ``notification.confirm'' method which can display multiple buttons (two, really). What’s also not explained is that this just calls out to the “notification.alert” method with a ``buttonText'' which is just a comma-delimited string with your button labels, like so:
delegate = navigator.notification.confirm("You done goofed!", "Back traced!", "Crap,Lulz");
This is exactly equivalent to the following:
delegate = navigator.notification.alert("You done goofed!", "Back traced!", "Crap,Lulz");
The only advantage of the ``notification.confirm'' call is that it makes your code more understandable and if you just want “OK” and “Cancel” you don’t have to pass in any parameters for buttonLabel.
Hope this helps anyone out who is looking for this information!