How to extract contact list having phone numbers and emails from iPhone contacts using Titanium
Posted By : Amit Saini | 12-Nov-2014
How to extract Phone numbers from iPhone.
On iOS, the contacts database may be modified by an external application, causing any Person or Group objects you've retrieved to be out of sync with the database. The IDs of these objects are not guaranteed to remain the same, so updating an object when it is out of sync may have unpredictable results.
To avoid this, listen for the reload event. When you receive a reload event, you should assume that any existing Person or Group objects are invalid and reload them from contacts module before modifying them.
fetchContacts = function() { spinner.show(); var performAddressBookFunction = function() { //Getting all the contacts if (args.isEmailsFetchingScreen) { data = fetchingEmails(); } else { data = fetchingPhoneNumbers(); } Ti.API.info("dddddd length " + data.length); //if contact length is zero than data.length=0 //or if Don't Allow for accessing contacts than by default data length is zero. Alloy.Globals.ContactDataLength = data.length; $.selectContactsTable.sections[0].setItems([]); $.selectContactsTable.sections[0].setItems(data); spinner.hide(); }; var addressBookDisallowed = function() { //alert("allows appdator to access contacts from device : settings-> privacy-> contacts -> switch on for appdator"); //if user don't allow for permission of contacts accessing. spinner.hide(); }; if (Ti.Contacts.contactsAuthorization == Ti.Contacts.AUTHORIZATION_AUTHORIZED) { //Ti.API.info("authorized 1 : Ti.Contacts.AUTHORIZATION_AUTHORIZED"); performAddressBookFunction(); } else if (Ti.Contacts.contactsAuthorization == Ti.Contacts.AUTHORIZATION_UNKNOWN) { //Ti.API.info("authorized 1 : Ti.Contacts.AUTHORIZATION_UNKNOWN"); Ti.Contacts.requestAuthorization(function(e) { //Ti.API.info("requestAuthorization process " + JSON.stringify(e)); if (e.success) { //Ti.API.info("authorized 2" + JSON.stringify(e)); performAddressBookFunction(); } else { //Ti.API.info("unauthorized 1"); addressBookDisallowed(); } }); } else if (Ti.Contacts.contactsAuthorization == Ti.Contacts.AUTHORIZATION_DENIED) { //Ti.API.info("unauthorized 2 Ti.Contacts.AUTHORIZATION_DENIED"); addressBookDisallowed(); //performAddressBookFunction(); } else { //Ti.API.info("unauthorized 2 Ti.Contacts.AUTHORIZATION_RESTRICTED"); addressBookDisallowed(); } }; fetchContacts();
function isEmpty(obj) { return Object.keys(obj).length === 0; } var fetchingPhoneNumbers = function() { var data = []; var people = Ti.Contacts.getAllPeople(); for (var i = 0, ilen = people.length; i < ilen; i++) { var person = people[i]; var title = people[i].fullName; if (!title || title.length === 0) { title = "No Name"; } Ti.API.info("person name : " + title); var personNumbers = []; //fetching phone numbers for (var temp in person.phone) { var temp_numbers = person.phone[temp]; for (var k = 0; k < temp_numbers.length; k++) { var temp_num = temp_numbers[k]; temp_num = temp_num.replace(/[^\d.]/g, ""); Ti.API.info("temp_num" + temp_num); personNumbers.push(temp_num); } } //Ti.API.info("phone numbers : "+JSON.stringify(person.phone)); if (!isEmpty(person.phone)) { data.push({ id : i, person_Name : { text : title }, check_Box : { image : '/images/icons/checkBoxUnchecked.png' }, hisPhoneNumbers : personNumbers }); } } //sorting of data array according to person name. data.sort(function(a, b) { if (a.person_Name.text > b.person_Name.text) { return 1; } if (a.person_Name.text < b.person_Name.text) { return -1; } // a must be equal to b return 0; }); _.each(data, function(e, index) { data[index].id = index; }); //Ti.API.info("data sorting :: " + JSON.stringify(data)); return data; };
How to extract Emails from iPhone Contacts.
function emailValidation(emailValue) { try { validate(emailValue, { email : true }); } catch (error) { return false; } return true; } var fetchingEmails = function() { var data = []; var people = Ti.Contacts.getAllPeople(); for (var i = 0, ilen = people.length; i < ilen; i++) { var person = people[i]; var title = people[i].fullName; if (!title || title.length === 0) { title = "No Name"; } Ti.API.info("person name : " + title); var personEmails = []; //this check is used for conforming that array will contain at least one email that is actual. var actualConfirmed = false; //fetching emails //Ti.API.info("person email::::1 " + JSON.stringify(person.email)); for (var temp in person.email) { var temp_emails = person.email[temp]; if (temp_emails && (temp_emails.length > 0)) { //Ti.API.info("person email::::2 " + JSON.stringify(temp_emails)); for (var k = 0; k < temp_emails.length; k++) { var temp_email = temp_emails[k]; var isActualEmail = emailValidation(temp_email); Ti.API.info("temp_email " + temp_email + " :::: isActualEmail " + isActualEmail); if (isActualEmail) { actualConfirmed = true; personEmails.push(temp_email); } } } } Ti.API.info("!isEmpty(person.email) && actualConfirmed " + !isEmpty(person.email) + " ::: " + actualConfirmed); if (!isEmpty(person.email) && actualConfirmed) { Ti.API.info("person email::::3 " + JSON.stringify(person.email)); data.push({ id : i, person_Name : { text : title }, check_Box : { image : '/images/icons/checkBoxUnchecked.png' }, hisEmails : personEmails }); } Ti.API.info("data 1st :: " + JSON.stringify(data)); } //sorting of data array according to person name. data.sort(function(a, b) { if (a.person_Name.text > b.person_Name.text) { return 1; } if (a.person_Name.text < b.person_Name.text) { return -1; } // a must be equal to b return 0; }); _.each(data, function(e, index) { data[index].id = index; }); Ti.API.info("data sorting :: " + JSON.stringify(data)); return data; };
Now Directly use these function in your project and you have the all contacts list with phone no. and emails.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Amit Saini
Amit is an iPhone and Android application developer with experience in Titanium Framework . Amit likes to play basketball ,volleyball and listens to music in his free time.