I need to fetch the contacts of iPhone in my app and I have written code for that. In some of the iPhones, few contacts are not genuine, I mean name is save as blank or by any random string like "6.w S" and in some number is not genuine. I want to fetch the contacts which are not like as shown in the image below as it is crashing my app. So what checks I need to put in order to avoid fetching contacts like that. I am using Swift and my code is also written below:
My Code:
var people = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(self.objAddressBook, nil, ABPersonGetSortOrdering()).takeUnretainedValue() as NSArray as [ABRecord]
for person in people
{
var multiPhones:ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()
for phoneNumbers in 0..<ABMultiValueGetCount(multiPhones)
{
var phoneNumber = ABMultiValueCopyValueAtIndex(multiPhones, phoneNumbers).takeRetainedValue() as! String
var nameOfPerson: String = (ABRecordCopyCompositeName(person).takeRetainedValue() as! String)
var personRecordDic = ["name":nameOfPerson,"phone":phoneNumber]
self.contactsArray.addObject(personRecordDic)
}
}
Issue Causing Contactss
While fetching the contact add validation to your required fields.
Eg. If you are using Number,Email of contact then check wether those field have blank value or not. If blank value will be there then don't add to your list.