Using a Pin Dialog in Your Ionic Framework Mobile App
Using the Apache Cordova PinDialog plugin by Paldom we can use native dialogs in our application and accept passwords.
Let’s start by creating a fresh Ionic Framework application using the Command Prompt (Windows) or Terminal (Mac / Linux):
With the project created, it is time to install the Apache Cordova plugin. With the project as the current working directory for your Command Prompt or Terminal, run the following command:
Now technically we can start using the plugin with raw JavaScript, but since Ionic Framework runs off AngularJS, we’re going to install the AngularJS extension set, ngCordova.
In this tutorial I’m going to be using the version of ngCordova from commit c3634c6 on GitHub. You’re welcome to be adventurous and use the latest version, but no guarantees that this tutorial will work with it.
With ngCordova downloaded, copy ng-cordova.min.js into your project’s www/js directory and open the www/index.html file so we can make it look similar to the following:
Notice that ng-cordova.min.js was included before the cordova.js line. This is very important because if you don’t, it might not function correctly. Also notice how
ng-controller="ExampleController"
is added as well. We’re going to check out that controller in a moment.
Now open your www/js/app.js file and alter the following line:
You’ll see that we’ve only added
ngCordova
to the array. The library is now ready for use.
With the file still open, add the following controller:
Because the plugin uses native device code we must first wrap it in an
$ionicPlatform.ready()
to make sure we don’t try to use it too early. You are welcome to use an onDeviceReady()
method instead.
In
ExampleController
we’ve statically set a password to compare against. When the user inputs a numeric value and accepts, the result will be compared against our static password and display a message accordingly.Conclusion
You’ve now seen two ways of adding pin protection in your Ionic Framework mobile Android and iOS application. In particular we’ve just seen how to protect the application using a native dialog versus a pure CSS and JavaScript implementation.
0 comments