While not an important feature, you can, using Cordova (or ngCordova in our case) access your smart devices contacts. Various applications like Twitter or LinkedIn are already using this feature to access a list of your friends, so can you.
 
I’m writing this tutorial to show you how easy it is, so just relax and keep reading.
 
 

Note: If this tutorial was helpful, you need further clarification, something is not working or you have a request for another Ionic post? Furthermore, leave me a comment below if you don't like something about this blog, if something is bugging you, don't like how I'm doing stuff here. Feel free to comment below, subscribe to my blog, mail me to dragan.gaic@gmail.com. Thanks and have a nice day!
 

Table of Contents

 
Click here if you want to see other tutorials, or articles about the Ionic framework (themes, templates, plugins, tutorials)

Tutorials

Other Resources (themes, templates, plugins, Cordova plugins, starter apps)

Cordova Plugin Tutorials

 

Preparation

 
This tutorial has some requirements; skip this step if you already have a configured framework environment:
 
  • Android Environment (or iOS if your working on a MacOS)
  • nodeJS
  • Ionic
  • Cordova
In case you don’t have a previous Ionic Framework experience, find more information here: Ionic Framework | Installation Guide.
 

1. Update Ionic & Cordova

 
Before we can update Ionic and Cordova make sure you have the latest nodeJs version. You’ll not be able to download/update latest Ionic Framework without it.
 
Now we can update Ionic and Cordova, older versions may not work with this tutorial:
 
npm update -g cordova ionic
 

2. Create a New Project

 
ionic start IonicContactsDemo blank
cd IonicContactsDemo
 
Warning: Since some of you never worked with Ionic CLI. From this point and further, every time I tell you to execute something, do that inside a project folder.
 
Lets add appropriate platforms to our project:
 
ionic platform add android
 
For MacOS users, do it like this:
 
ionic platform add ios
 

3. Download ngCordova archive

 
ngCordova is a collection of 63+ AngularJS extensions on top of the Cordova API that make it easy to build, test, and deploy Cordova mobile apps with AngularJS. While we can make this work without ngCordova, it’s much more practical this way. This collection also holds many Ionic Framework compatible plugins.
 
Download ngCordova zip file, here’s site link:
 
Official Site Docs Download Link
 
 
Unzip this archive, find a file name called: ng-cordova.min.js, it should be available at this location: ng-cordova-master\dist\.
 
Open your newly created project directory and copy ng-cordova.min.js to this location: IonicContactsDemo\www\js.
 
Now include this location to index.html located at IonicContactsDemo\www; add it above cordova.js file, like this:
 
<!-- cordova script (this will be a 404 during development) -->
<script src="js/ng-cordova.min.js"></script>
<script src="cordova.js"></script>
 

4. Include Cordova Contacts Plugin

 
Add required plugin:
 
cordova plugin add cordova-plugin-contacts
cordova plugin add cordova-plugin-whitelist
 
Install Whitelist plugin only if you’re using Cordova 4.0 +. Without it you’ll receive “Application Error: There was a network error.” error.
 
GitHub - Contacts
 
 
 

Development

 
At this point, you should have everything set up and ready, we can start working on our example.
 
Go to IonicContactsDemo project directory and find app.js, alter angular.module line and include ngCordova service, like this:
 
var app = angular.module('starter', ['ionic', 'ngCordova'])
 
Open index.html file, add a controller to ion-content directive:
 
  <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Local Notification Example</h1>
      </ion-header-bar>
      <ion-content ng-controller="ContactsController">

      </ion-content>
   </ion-pane>
 
Initialize this controller in app.js file; don’t forget to inject $cordovaContacts service.
 
app.controller('ContactsController', function($scope, $cordovaContacts, $ionicPlatform) {

});
 
If you’re using Cordova 4+ you will need to check if there’s a Whitelist plugin installed with your project. If you have it, add this security meta tag to your index.html HEAD content:
 
<meta http-equiv="Content-Security-Policy" content="default-src *; font-src data: *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src  'self' 'unsafe-inline' *">
 
Without it your application will throw an error during the app initialization, after that it will constantly throwing no security meta-tag found. While not a blocking point, constant console log warnings will make it hard to do any debugging.
 
If you want to create a contact use this function:
 
$scope.addContact = function() {
	$cordovaContacts.save($scope.contact).then(function(result) {
		console.log('Contact Saved!');
	}, function(err) {
		console.log('An error has occured while saving contact data!');
	});
};
 
$scope.contact will hold all information about a new contact. Contact properties can be found in a example below.
 
If you want to find a contact(s) use this function:
 
$scope.getAllContacts = function() {
	$cordovaContacts.find({filter : 'Robert', fields:  [ 'displayName']}).then(function(allContacts) { //omitting parameter to .find() causes all contacts to be returned
		$scope.contacts = allContacts;
		console.log(JSON.stringify(allContacts));
	});
};
 
In case you want to return every stored contact the find function should be used like this:
 
.find({filter : ''}).then
 
If you want to delete a contact use this function:
 
$scope.removeContact = function() {
	
	$scope.removeContact = {};   // We will use it to save a contact
	$scope.removeContact.displayName = 'Gajotres'; // Contact Display Name			
	
	$cordovaContacts.remove($scope.removeContact).then(function(result) {
		console.log('Contact Deleted!');
		console.log(JSON.stringify(result));
	}, function(error) {
		console.log('An error has occured while deleting contact data!');
		console.log(JSON.stringify(error));
	});
}	
 
Continue Reading