I would like to store contact from Address Book in NSDictionary.
I succeed to Log First name + Last name of contact and store in NSDictionary "name". But when smiley or some accent like "é" or "è" is in the first or last name, that gives me error like this : "Name = "Manon \Ud83d\Udc9c (null)";"
And second error : when the contact has multiple number, that stores the number in my NSDictionary "number" but gives me error like this "Number = "06\U00a067\U00a054\U00a069\U00a034";"
Maybe use something for searching only MOBILE number (beginning with "06" or "+336" in France) ? and add it to the NSDictionary "number" ?
How can I fix it ?
Here is my code in ViewDidLoad :
//ADDRESS BOOK
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
if (addressBook != NULL) {
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted)
{
NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
//The array in which the first names will be stored as NSStrings
// self.allContact = [[NSMutableArray alloc] init];
for(NSUInteger index = 0; index <= ([arrayOfPeople count]-1); index++){
ABRecordRef person = (__bridge ABRecordRef)[arrayOfPeople objectAtIndex:index];
NSString *currentFirstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *currentLastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *allName = [NSString stringWithFormat:@"%@ %@", currentFirstName, currentLastName];
[self.allContact addObject: allName];
self.contact = [NSMutableDictionary
dictionaryWithDictionary:@{
@"Name" : @"Number"
}];
[self.contact setObject:allName forKey:@"Name"];
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
if (phones != NULL)
{
for (NSInteger index = 0; index < ABMultiValueGetCount(phones); index++)
{
NSString *phone = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, index));
[self.contact setObject:phone forKey:@"Number"];
}
}
NSLog(@"%@", self.contact);
}
//OPTIONAL: The following line sorts the first names alphabetically
NSArray *sortedFirstNames = [self.allContact sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
// NSLog(@"%@", sortedFirstNames);
}
});
}
"Name = "Manon \Ud83d\Udc9c (null)";"
"Number = "06\U00a067\U00a054\U00a069\U00a034"
It's not an error. \U00a067
and others are unicode symbols. Console just doesn't know how to display such symbols ("é" or "è"), that's why it displays unicode symbols.
However in application these symbols will be displayed as expected.
I have the same behavior with Russian characters all the time.