但请记住,在Ionic中,如果要使用本机API,必须首先等待
platform.ready()
此事件将通知所有内容都已加载并且可以使用。
import { Platform } from 'ionic-angular';
import { Contacts, Contact, ContactField, ContactName } from '@ionic-native/contacts';
constructor(private contacts: Contacts, private plt: Platform) {
this.plt.ready().then((readySource) => {
console.log('Platform ready from', readySource);
// Platform now ready, execute any required native code
this.initContacts();
});
}
initContacts(): void {
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)
);
// If you want to open the native contacts screen and select the contacts from there use pickContact()
this.contacts.pickContact()
.then((response: Contact) => {
console.log(response)
});
}