0

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);


        }
    });
}
Vlad Papko
  • 13,184
  • 4
  • 41
  • 57
Vjardel
  • 1,065
  • 1
  • 13
  • 28
  • 1
    "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 instead. – Vlad Papko Jul 28 '15 at 21:18
  • @Visput How can I display it properly ? I don't succeed to display NSString *string = [NSString stringWithUTF8String:"%@", (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty)]; "Too many arguments" – Vjardel Jul 28 '15 at 21:21
  • I think you can't display it properly in console. But in application it will be displayed as expected. – Vlad Papko Jul 28 '15 at 21:22
  • @Visput I think we can with [NSString stringWithUTF8String], but I can't make it work with this argument : (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty). How can I make it ? – Vjardel Jul 28 '15 at 21:23
  • No, `[NSString stringWithUTF8String]` - requires C-array of bytes. `ABRecordCopyValue` returns CFString that is bridged to NSString. – Vlad Papko Jul 28 '15 at 21:25
  • @Visput Ok :( thanks man! – Vjardel Jul 28 '15 at 21:26
  • I added my comment to answer, since it makes clear for you. – Vlad Papko Jul 28 '15 at 21:31

1 Answers1

1

"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.

Vlad Papko
  • 13,184
  • 4
  • 41
  • 57