I want to check if the number registered on my App and stored on my server are on the device where the app is running. I tried but I really can't go forward from here:
In my class I have declared:
var chatMatesArray: NSMutableArray = NSMutableArray()
var contactsName: NSMutableArray = NSMutableArray()
var users: NSMutableArray = NSMutableArray()
var phoneNumbers: NSMutableArray = NSMutableArray()
And in my viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
//I already have the permission to check AddressBook
var allNumbers: [AnyObject] = []
let adbk : ABAddressBook? = ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue()
let people = ABAddressBookCopyArrayOfAllPeople(adbk).takeRetainedValue() as NSArray as [ABRecord]
for person in people {
let phones: ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()
for j in 0..<ABMultiValueGetCount(phones) {
let phone: String = ABMultiValueCopyValueAtIndex(phones, j).takeRetainedValue() as! String
allNumbers.append(phone)
self.phoneNumbers.addObject(allNumbers)
}
}
}
Then I have a func that retrieve number from my server, I store them into the array chatMatesArray and then I try to do:
let set1 = Set(arrayLiteral: self.chatMatesArray)
let set2 = Set(arrayLiteral: self.phoneNumbers)
self.users.addObject(set2.intersect(set1))
This would check numbers that are both on my server and on the addressBook, right? But then how can I go back from that number to their owner full name so that I can show in a UITableView?
Thanks in advance for your help.
For your approach, you could use a dictionary that maps a phone number to an contact (although you'll probably have collisions).
But this really isn't the Swift way of doing things. You should look into using map
and filter
to transform the address book data and return the set of contacts that (don't) match your criteria.
If you prefer the original approach, it would be simpler to fast enumerate through the list of contacts looking for matches, rather than doing the set intersection then having to map the phone numbers back to contacts.