Contacts Plugins In Ionic 5 Application
Tags: android, angular, cordovaplugin, ionicframework
Published: 2022-06-21 04:06:33

---
title: Contacts Plugins In Ionic 5 Application
published: true
date: 2022-04-11 04:35:50 UTC
tags: android,angular,cordovaplugin,ionicframework
canonical_url: https://enappd.com/blog/contact-plugin-in-ionic-angular-application/187/
---

### Contacts Plugins In Ionic Angular Application

![](https://cdn-images-1.medium.com/max/1024/1*7256mCwrPAsbaanZB4dBoA.png)

In this tutorial, we will learn how to implement Cordova’s [Contacts Plugin](https://ionicframework.com/docs/native/contacts) in the Ionic 5 Angular Application on Android Phone (you can use the same plugin on other platforms also). Contact Plugin helps to implement various functionality like getting contact from the Phone’s contact list, and saving new contact info into the phone’s native contact list. Implementing this Plugin is quite simple and easy, only we have to Inject the plugin dependency and use it in the required Pages.

We will be implementing, a simple Contacts App with the below stated features:-

1. Add contacts from Native Contact List of phone and save it into Local Storage.
2. Creating new Contact and saving them into the Native Contact List.

### What is Ionic?

Skip this section if you know about Ionic.

You probably already know about Ionic, but I’m putting it here just for the sake of beginners. **Ionic** is a complete open-source SDK for hybrid mobile app development created by [Max Lynch](https://medium.com/u/a76fa51de1ba), [Ben Sperry](https://medium.com/u/14b1be5c1354) and Adam Bradley of Drifty Co. in 2013. Ionic provides tools and services for developing hybrid mobile apps using Web technologies like CSS, HTML5, and Sass. Apps can be built with these Web technologies and then distributed through native app stores to be installed on devices by leveraging Cordova.

So, in other words — If you create Native apps in Android, you code in Java. If you create Native apps in iOS, you code in Obj-C or Swift. Both of these are powerful but complex languages. **With Cordova (and Ionic) you can write a single piece of code for your app that can run on both iOS and Android** (and windows!), that too with the simplicity of HTML, CSS, and JS.

### Creating the Contacts App

We will follow some easy steps to implement Contacts Plugin in the Ionic 5 Angular application:-

1. Creating Ionic 5 Angular Project.
2. Installing Contacts Plugin in Ionic 5 Angular Application and adding the Contacts Plugin Logic.
3. Building the application on the Android Platform.

So let's start to code and get our app ready.

![Code, code and code….!!](https://cdn-images-1.medium.com/max/360/1*joplbbiO8N8X-L4Pfj0-jg.gif)


### Step 1: Creating Ionic 5 Angular Project

To create an Ionic 5 Angular app, we just have to run one command and that’s it (Assuming you already have Ionic CLI set up on your terminal, else read our [blogs](https://enappd.com/blog/how-to-create-an-ionic-app-for-beginners/144/)). The rest of the things will be handled by the Ionic CLI. And in Ionic 5, we have got various options to choose various frameworks like (Angular, React, and Vue). In this tutorial, we will use Angular to build our apps. Run the below command to create the Ionic 5 Angular app.

```
$ ionic start <appName> blank
```

Replace **appName** with the name you want for your project. If you are a beginner and want to know more about Ionic 5. You can go and visit this [BLOG](https://enappd.com/blog/how-to-create-an-ionic-app-for-beginners/144/). A new project will be created in your working directory with the name you used. Now the Ionic project is created, we can integrate the Cordova **Contacts Plugin** into the App.

### Step 2: Installing Contacts Plugin in Ionic 5 Application

Before installing the plugin we have to add the native mobile platforms in the Ionic 5 application, to install the platform you can run the below command in Command Line/Terminal:-

```
$ ionic cordova platform add <Platform_name>
```

You have replaced the **Platform\_name** with the **android** or **ios** , whichever you want. In this case, we will add Android as the platform and continue to install the Contacts Plugin.

To install the [Contacts Plugin](https://ionicframework.com/docs/native/contacts/), we just have to run few simple commands in Command Line/Terminal :

```
$ ionic cordova plugin add cordova-plugin-contacts
$ npm install @ionic-native/contacts
```

Now the plugin is installed, we are good to go to add the Logic in our Ionic 5 application. First, we will import and inject the Contacts Plugin in **app.module.ts** _[Line 19 at app.module.ts] _— Imported Contacts Plugin and at line 37 — Injected Contacts Plugin.

{% gist https://gist.github.com/vaibhavgehani/8a8e2e0df1a041592254c1d8841a76e6 %}

After injecting the Contacts Plugin, we can use it in any of the Page or Components. First, we will implement the reading contact information feature.

#### 1. Getting Contact From Native Contact List using Contacts Plugin:-

To implement the Reading feature, we will use a few buttons which will take the contact from the Native contact list.

To make this page, first, we will add the HTML code in the Ionic 5 application.

We will use the **add-contact** page to add the native contact to local storage, you can generate the page using the below command (in the Ionic root directory):-

```
$ ionic generate page add-contact
```

The above command will add the **add-contact** page in your working project, now will add the HTML code to **add-contact.page.html** :-

{% gist https://gist.github.com/vaibhavgehani/8f21c0045520b873e10fe7a7b1e177f0 %}

Above HTML will look like below Image:-

![create contact page](https://cdn-images-1.medium.com/max/277/1*I9-4J_O3EKOIfauyt2DyJQ.jpeg)


In the above HTML code, we have simply added 2 buttons — ** +Pick from Contact List** & **Create Contact** button and a form using which we can save contact directly in the Local Storage. Now we will look at the logic placed in add-contact.page.ts:-

{% gist https://gist.github.com/vaibhavgehani/be4987ecd53a51b31030d8d26435995d %}

In the above code, we have simply injected the Contacts Plugin and used to get the contact from the Native List using **_contactList_() function** [Line 36]. This function is attached to the **+ Pick from Contact List** Button. In the **_contactList_** () function we have used the **_pickContact_()** method [Inside the contactList() method], which will pick the contact (this will ask for permission from the user on phone) from the Native Contact List.

Once the Contact is selected, then the app will ask for the Confirmation using an Alert defined in **showConfirmationAlertForContacts()** function [line 95]. Once you confirm it, the selected contact will be saved into the Local Storage. Most of the Local Storage contacts logic is defined under **Contact Services** , to create contacts services you can run the below command:-

```
$ ionic generate services contact
```

The above command will create a contact service in the working directory of the Ionic 5 project. Below is the code for the Contact Service file:-

{% gist https://gist.github.com/vaibhavgehani/baf72de544b9843f5072c673bc3b6ec9 %}

To add the contact in the Local storage, we will call the **_saveContactToLocal_** () function defined in **contact.service.ts** file.

{% gist https://gist.github.com/vaibhavgehani/22adc1bc33f39e27f59812e2d628af39 %}

The above command will add the selected contact in the Local Storage with the following (_id, contact\_name, contact\_email, contact\_photo, contact\_number_) attributes.

#### 2. Reading Saved Contact from Local Storage:-

To show the contact that we have read from the local storage. You can add a new page called the _Contacts page_, by running the below command in the Ionic 5 project:-

```
$ ionic generate page contacts
```

The above command will add the contacts page in your working project, now will add the HTML code to **contacts.page.html**  :-

{% gist https://gist.github.com/vaibhavgehani/61bbcb6b386ff6b1747421c04179f064 %}

In the above code, we have simply rendered the array of contacts [**_Line 12 in contact.service.ts file_**] defined under contact services. The HTML will look like the below image:-

![Reading Contact](https://cdn-images-1.medium.com/max/278/1*LMARw7mF22ugAt5LQ_p6vQ.jpeg)


Now we will look at the Logic used in the _Contacts page_ (i.e. contacts.page.ts), below is the code for the **contact.page.ts** file.

{% gist https://gist.github.com/vaibhavgehani/0bb5ce636d08c0c4c8acc760058886eb %}

In the above code, we call the **_getAllSavedContact_()** _[Line 20 in contacts.page.ts]_ function as soon as the Contact Page gets loaded into the View. To do that we have Ionic lifecycle method **_ionViewDidEnter_()** _[Line 19 in contacts.page.ts]_. This function takes all the Contacts from Local Storage and pushes them into the array.

{% gist https://gist.github.com/vaibhavgehani/192d204f0c6db8fce10675972ef680b5 %}

Now we have implemented reading of contacts from Native Contact List and added the selecting contact list in Local Storage. In Addition, you can save the contact to the Native Contact List as well by using a simple function mentioned below:-

For this, Import the Plugin using the below line:-

```
import { Contacts } from '@ionic-native/contacts/ngx';
```

After Importing the plugin, we can inject it into the component using the below code:-

```
Constructor(private contacts: Contacts) {}
```

Now we can add the below code to create a new contact into the Phone Contact List.

Constructor Params:-  
1) new ContactName(nick\_name, first\_name, last\_name)  
2) new ContactField(‘mobile’, mobile\_number)

```
let contact: Contact = this.contacts.create();
contact.name = new ContactName(null, 'Smith', 'John');
contact.phoneNumbers = [new ContactField('mobile', '6471234567')];
contact.save().then(
  () => console.log('Contact saved!', contact),
  (error: any) => console.error('Error saving contact.', error)
);
```

### Step 3: Building the application on the Android Platform.

To add and build the Android application, run the below commands :-

```
$ ionic cordova platform add android
$ ionic cordova run android
```

The below video is for a completed application, the same flow can be seen in your application also.

![](https://cdn-images-1.medium.com/max/480/1*992_yAid73aW08LmlJHogw.gif)

### Conclusion

In this tutorial, we have learned about implementing the Contacts Plugin in the Ionic 5 Angular application. Contacts Plugin can be used in any of the business logic applications like taxi app (calling the driver), Data Sharing app (sharing data to contacts), and many more. So now you can integrate this feature in your Ionic app and take it to another level. And if you want to add/learn more features you can visit [**_Enappd Blog_**](https://enappd.com/blog/).

### Next Steps

If you liked this blog, you will also find the following Ionic blogs interesting and helpful. Feel free to ask any questions in the comment section

- Ionic Payment Gateways — [Stripe](https://enappd.com/blog/ionic-4-stripe-payment-integration-with-firebase-for-apps-and-pwa/17) | [PayPal](https://enappd.com/blog/ionic-4-paypal-payment-integration-for-apps-and-pwa/16) | [Apple Pay](https://enappd.com/blog/how-to-integrate-apple-pay-in-ionic-4-apps/21) | [RazorPay](https://enappd.com/blog/how-to-integrate-razorpay-in-ionic-4-apps-and-pwa/20)
- Ionic Charts with — [Google Charts](https://enappd.com/blog/ionic-4-charts-using-google-charts/66)| [HighCharts](https://enappd.com/blog/adding-charts-in-ionic-4-apps-and-pwa-part-3/60) | [d3.js](https://enappd.com/blog/adding-charts-in-ionic-4-apps-and-pwa-part-2/54) | [Chart.js](https://enappd.com/blog/charts-in-ionic-4-apps-and-pwa-part-1/52)
- Ionic Social Logins — [Facebook](https://enappd.com/blog/facebook-login-in-ionic-4-apps-using-firebase/25) | [Google](https://enappd.com/blog/google-login-in-ionic-4-apps-using-firebase/39) | [Twitter](https://enappd.com/blog/twitter-login-in-ionic-4-apps-using-firebase/24)
- Ionic Authentications — [Via Email](https://enappd.com/blog/email-authentication-with-firebase-in-ionic-4/38) | [Anonymous](https://enappd.com/blog/firebase-anonymous-login-in-ionic-4/37)
- Ionic Features — [Geolocation](https://enappd.com/blog/using-geolocation-geocoding-and-reverse-geocoding-in-ionic-4/45) | [QR Code reader](https://enappd.com/blog/qr-code-scanning-and-optical-character-recognition-ocr-in-ionic-4/9)| [Pedometer](https://enappd.com/blog/best-fitness-plugins-for-ionic-4-how-to-use-pedometer/15)
- Media in Ionic — [Audio](https://enappd.com/blog/spotify-like-music-in-ionic-4-apps/48) | [Video](https://enappd.com/blog/adding-video-player-in-ionic-4-app/64) | [Image Picker](https://enappd.com/blog/image-picker-in-ionic-4-app/67) | [Image Cropper](https://enappd.com/blog/how-to-add-image-cropper-in-ionic-4-app/68)
- Ionic Essentials — [Native Storage](https://enappd.com/blog/ionic-4-save-and-retrieve-data-locally-on-device/59) | [Translations](https://enappd.com/blog/how-to-translate-in-ionic-4-globalization-internationalization-and-localization/11) | [RTL](https://enappd.com/blog/rtl-right-to-left-use-in-ionic-4/50)
- Ionic messaging — [Firebase Push](https://enappd.com/blog/implement-ionic-4-firebase-push/34) | [Reading SMS](https://enappd.com/blog/automatically-read-sms-in-ionic-4-apps/42)
- Ionic with Firebase — [Basics](https://enappd.com/blog/how-to-integrate-firebase-in-ionic-4-apps/23) | [Hosting and DB](https://enappd.com/blog/firebase-with-ionic-4-hosting-auth-and-db-connection/58) | [Cloud functions](https://enappd.com/blog/firebase-cloud-functions-in-ionic-4-complete-guide/32)

### Ionic React Full App with Capacitor

If you need a base to start your next Ionic 5 React Capacitor app, you can make your next awesome app using [Ionic 5 React Full App in Capacitor](https://store.enappd.com/product/ionic-react-full-app-capacitor/)

![Ionic 5 React Full App in Capacitor from Enappd](https://cdn-images-1.medium.com/max/760/1*G3yOFVX4oABx9MhUy-3kng.png)

### Ionic Capacitor Full App (Angular)

If you need a base to start your next Angular **Capacitor app** , you can make your next awesome app using [Capacitor Full App](https://store.enappd.com/product/capacitor-full-app-with-ionic-angular/)

![Capacitor Full App with huge number of layouts and features](https://cdn-images-1.medium.com/max/736/0*UwSHcofTMStMnzAX.png)

### Ionic Full App (Angular and Cordova)

If you need a base to start your next Ionic 5 app, you can make your next awesome app using [Ionic 5 Full App](https://store.enappd.com/product/ionic-full-app)

![Ionic Full App with huge number of layouts and features](https://cdn-images-1.medium.com/max/700/0*1ArpKyhiDHZnevwB.jpeg)


* * *