How to access address book / contacts on iOS?

#=================Tutorial================

I wrote a tutorial on how to use ngCordova Contacts plugin to create, find, and delete contacts. You will also find a working example.

##Click here to open a tutorial including working examples

You can use these functions:

$ionicPlatform.ready(function() {	

    $scope.contacts = {};  // We will use it to load contacts 	
	
	
	$scope.contact = {     // We will use it to save a contact

		"displayName": "Gajotres",
		"name": {
			"givenName"  : "Dragan",
			"familyName" : "Gaic",
			"formatted"  : "Dragan Gaic"
		},
		"nickname": 'Gajotres',
		"phoneNumbers": [
			{
				"value": "+385959052082",
				"type": "mobile"
			},
			{
				"value": "+385914600731",
				"type": "phone"
			}				
		],
		"emails": [
			{
				"value": "dragan.gaic@gmail.com",
				"type": "home"
			}
		],
		"addresses": [
			{
				"type": "home",
				"formatted": "Some Address",
				"streetAddress": "Some Address",
				"locality":"Zagreb",
				"region":"Zagreb",
				"postalCode":"10000",
				"country":"Croatia"
			}
		],
		"ims": null,
		"organizations": [
			{
				"type": "Company",
				"name": "Generali",
				"department": "IT",
				"title":"Senior Java Developer"
			}
		],
		"birthday": Date("08/01/1980"),
		"note": "",
		"photos": [
			{
				"value": "https://pbs.twimg.com/profile_images/570169987914924032/pRisI2wr_400x400.jpeg"
			}
		],
		"categories": null,
		"urls": null
	}		
			
    $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!');
        });
    };

    // This function can take some time  so be patient
    $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));
        });
    };

    $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));
        });
    }		  

});

Also don’t forget to wrap everything into Ionic ready event.

More complex version can be found in a provided tutorial. Leave me a message if you have more question or if you think something is missing.

1 Like