Skip to content

Instantly share code, notes, and snippets.

@marionava
Last active May 2, 2020 10:38
Show Gist options
  • Save marionava/eb8203bb1cf5cabb1e254ab034f655f6 to your computer and use it in GitHub Desktop.
Save marionava/eb8203bb1cf5cabb1e254ab034f655f6 to your computer and use it in GitHub Desktop.
Crear un contacto en Ionic con plugin
import { Contacts, Contact, ContactField, ContactName } from '@ionic-native/contacts/ngx';
misContactos: Contact[] = [];
constructor(
// tslint:disable-next-line: deprecation
private contacts: Contacts
) { }
importarContactos() {
const opciones = {
filter: '',
multiple: true
};
this.contacts.find(['*'], opciones).then((contacts: Contact[]) => {
this.misContactos = contacts;
console.log('Contactos', contacts);
});
}
crearContacto() {
const contact: Contact = this.contacts.create();
contact.name = new ContactName(null, 'Nava', 'Mario');
contact.phoneNumbers = [new ContactField('mobile', '7771345756')];
contact.save().then(
() => console.log('Contacto guardado!', contact),
(error: any) => console.error('Error al guardar contacto.', error)
);
}
// En el archivo .html
<ion-button (click)="importarContactos()" expand="full" shape="round">
Importar Contactos
</ion-button>
<ion-button (click)="crearContacto()" expand="full" shape="round">
Crear Contacto
</ion-button>
<ion-list>
<ion-item *ngFor="let c of misContactos">
<ion-label>
{{ c.name.givenName }} <strong>{{ c.name.familyName }}</strong>
</ion-label>
</ion-item>
</ion-list>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment