I use this tutorial http://austinknight.net/ionic-framwork-import-phone-contacts/ and so my code is :
function onSuccess(contacts) {
for (var i = 0; i < contacts.length; i++) {
var contact = contacts[i];
$scope.phoneContacts.push(contact);
}
$ionicLoading.hide();
};
function onError(contactError) {
alert(contactError);
};
var options = {};
options.multiple = true;
$cordovaContacts.find(options).then(onSuccess, onError);
In this code, all contacts will push to $scope.phoneContacts
, I need just contacts with mobile phone number, I checked the contact
variable in console.log
and I found the numbers stores in phoneNumbers
property, but this property is an array and in this array we have type
property which said this number is mobile or no,
this is the sample record:
[Log] Object (console-via-logger.js, line 173)
$$hashKey: "object:52"
addresses: Array[1]
birthday: 254145600000
categories: null
displayName: null
emails: Array[2]
id: 1
ims: null
name: Object
nickname: null
note: null
organizations: Array[1]
phoneNumbers: Array[2]
0: Object
id: 0
pref: false
type: "mobile"
value: "(555) 564-8583"
__proto__: Object
1: Object
length: 2
__proto__: Array[0]
photos: null
rawId: null
urls: null
__proto__: Object
Because phoneNumbers
is an array, I don't know how can I found contacts with type=mobile
.
Verify if each contact has a mobile number before adding to your scope var.
function contactHasMobileNumber(contact) {
for (var i = 0; i < contact.phoneNumbers.length; i++) {
var phoneNumber = contact.phoneNumbers[i];
if (phoneNumber.type == "mobile") {
return true;
}
}
return false;
}
Then update your existing function to check for it.
for (var i = 0; i < contacts.length; i++) {
var contact = contacts[i];
if (contactHasMobileNumber(contact)) {
$scope.phoneContacts.push(contact);
}
}
Please Update your code in following way :
function onSuccess(contacts) {
for (var i = 0; i < contacts.length; i++) {
var contact = contacts[i];
if(contact.phoneNumbers != null)
$scope.phoneContacts.push(contact);
}
$ionicLoading.hide();
};
function onError(contactError) {
alert(contactError);
};
var options = {};
options.multiple = true;
$cordovaContacts.find(options).then(onSuccess, onError);