Just a quick question. In the docs for the native contacts plugin there’s a description for an id property on the IContactProperties object. I’d like to set the id before saving a contact to the address book so I can look it up later on.
I can add a value to the object’s property like this:
contact.id = xxxxxxxx-xxxx-xxxx-xxxx
But when I read the saved contact back from the iOS device’s address book the id has been set to an internal id which auto-increments.
Does this mean a contact’s id can’t be set? If so, does anyone have any other suggestions?
I created a new app with the cli and added only the contacts plugin for testing purposes. Here’s the controller:
import { Component } from '@angular/core';
import { IonicPage, NavController } from 'ionic-angular';
import { Contacts, Contact, ContactField, ContactName } from '@ionic-native/contacts';
@IonicPage()
@Component({
selector: 'page-welcome',
templateUrl: 'welcome.html'
})
export class WelcomePage {
allContacts: any;
constructor(public navCtrl: NavController, private contacts: Contacts) { }
addContact = () => {
let contact: Contact = this.contacts.create();
contact.id = "1234567890";
contact.name = new ContactName(null, 'Smith', 'John');
contact.phoneNumbers = [new ContactField('mobile', '6471234567')];
contact.save().then(
() => { console.log('Contact saved!');console.dir(contact); }, /* <<< renders an _objectInstance to the console in wich the id is just incremented eg. 23 or something */
(error: any) => console.error('Error saving contact.', error)
);
}
findContacts = () => {
this.contacts.find(['id', 'displayName', 'name', 'phoneNumbers', 'emails'], {filter: "", multiple: true})
.then(data => {
this.allContacts = data;
console.log('allContacts: ');
console.dir(this.allContacts); /* <<< renders an array of _objectInstances to the console in wich the id for the added contact is just incremented eg. 23 or something */
});
}
}
I have come across a thread where it’s done in the bare cordova plugin. I can’t find any examples of people using this in Typescript with the ionic Native plugin. Does anyone have experience setting contact.id to the address book via the Ionic Native plugin wrapper?