import { Contacts } from '@ionic-native/contacts/ngx';
constructor( private contactsPlugin: Contacts ) { }
this.contactsPlugin.find(["displayName", "phoneNumbers","photos"], {multiple: true, hasPhoneNumber: true})
.then((contacts) => {
console.log("Length: ", contacts.length);
for(let i=0; i < contacts.length; i++){
console.log('... ',contacts[i]);
}
},
(error) => {
console.error("Contacts error: ", error)
})
I am using,
“cordova-plugin-contacts”: “3.0.1”,
“@ionic-native/contacts”: “^5.0.0”
First, make sure the platform is ready before calling it.
Second, for iOS you need to adjust the config.xml to request permission to access your contacts.
Thank you for reply. I tried after device ready but still not working.
document.addEventListener("deviceready", () => {
this.contactsPlugin.find(["displayName", "phoneNumbers","photos"], {multiple: true, hasPhoneNumber: true})
.then((contacts) => {
console.log("Length: ", contacts.length);
for(let i=0; i < contacts.length; i++){
console.log('... ',contacts[i]);
if ( contacts[i].displayName != null ) {
console.log("--> ", contacts[i].displayName );
}
}
},
(error) => {
console.error("Contacts error: ", error)
})
});
Solved:
import { Contacts } from '@ionic-native/contacts/ngx';
declare var navigator: any;
@Component({
selector: 'assignment',
templateUrl: './assignment.page.html',
styleUrls: ['./assignment.page.scss']
})
async getContacts() {
await document.addEventListener("deviceready", () => {
navigator.contacts.find(
[navigator.contacts.fieldType.displayName],
gotContacts,
errorHandler);
function errorHandler(e) {
console.log("errorHandler: "+e);
}
function gotContacts(deviceContacts) {
console.log("gotContacts, number of results " + deviceContacts.length);
for(var i=0, len = deviceContacts.length; i<len; i++) {
console.log("Contact >>> ", deviceContacts[i]);
}
}
}
}