How to Integrate Contact Plugin of Ionic

Thanks to Ionic's documentation, many developers struggle in integrating it's native API. Today I am gonna walk you through how to integrate and use Contact API of Ionic. I have used Ionic (version 3) with Angular ( Version 4) to implement this feature .

1. Installation of Contact Plugin

 Use following commands to install Contact plugin and corresponding node packages.These commands will update your package.json and config.xml.
$ ionic cordova plugin add cordova-plugin-contacts
$ npm install --save @ionic-native/contacts

2. Adding Plugin In App Module

    a. Import Contact plugin in your app.module.ts file
import { Contacts } from '@ionic-native/contacts';

    b. Add Contacts entry in list of provides in your app.module.ts file
providers:[
    ...
    Contacts
    ...
]

3. Use Contact plugin into Component

    a. Import Contact plugin in your xyz.component.ts file
import { Contacts } from '@ionic-native/contacts';

    b. Add Contacts reference in constructor
constructor(
    ...
    private contacts: Contacts,
    ...
) {}

    c. Write a function to use native Contact picker
pickContact() {
    this.contacts.pickContact().then((contact) => {
        this.name = contact.displayName;
    });
}

4. Template

<button ion-button (click)="pickContact();">
     {{transaction.accountability.title}}
</button>

By following steps above you will able to integrate Ionic Contact API into your application. It will add a button on your page on clicking on which Contact Picker will be launched. After selected a contact from the Contact Picker, It will display the contact name onto the button.

Comments

Popular posts from this blog

Cheat Sheet - Git

Stack implementation using VanillaJS

JavaScript Best Practices