Native Dialog fallback in serve

I was using a simple javascript confirm() dialog in my app when I discovered that on an iOS device, the confirm dialog title says “index.html”. So I’m going to switch to the cordova native dialogs plugin. My only problem now is that when I do a ionic serve it throws a runtime error “Uncaught (in promise): cordova_not_available”

I get that ionic serve can’t use native plugins, but is there any way that I can use the Dialogs plugin and have it fallback to a javascript confirm when the plugin isn’t available? or is there a different approach I could use? This is just a simple confirmation that the user wants to delete something.

My current approach: I’ve created a DialogService object with a confirmDialog method:

	confirmDialog( message : string, title : string ) {
	return Dialogs.confirm( message, title ).then( (userResponse) => {
		return new Promise( (resolve, reject) => {
			if (userResponse == 1) {
				resolve(true);
			} else {
				resolve(false);
			}
		});
	}).catch( (err) => {
		return new Promise( (resolve, reject) => {
			resolve(confirm(message));
		});
	});
}

And then in my page (after injecting the dialog service):

this.dialogService.confirmDialog("Are you sure you want to delete this message?", "Confirm").then(
		(confirmation) => {
			if (confirmation) {
				// do deletion here
			}
		}
	);

This seems to work, albeit I feel like it’s a little much just for a simple dialog…But it works on devices and when serving to browser

You can query the Platform module to resolve which environment you are running under. Alternatively, you could use Ionic’s AlertController

I knew I was overlooking something simple. The Alert component should work just fine.