0

I'm reading all the phone-numbers from the AdressBook api, and in some cases i've got a format like this: "(06)\U00a057\U00a011\U00a042\U00a095"

When checking my own addressbook however, it gives me a regular format like 0624567899

How come and how do I solve this?

bdv
  • 1,154
  • 2
  • 19
  • 42

2 Answers2

0

I had the same problem, saw your post while searching for a solution, then i thought that maybe i should unpack the array instead of just NSLog(@"Phone = %@", self.array). Therefore you have to unpack or iterate through your array and your values will show correctly:

for (NSString *d in self.array) {
        NSLog(@"Phone = %@", d);

)

RubyGladiator
  • 1,765
  • 3
  • 18
  • 27
0

I order to fetch phone numbers or email from the Address book try this

-(NSArray*)fetchAddressBook
{
NSMutableArray* arrAddressBook= [[NSMutableArray alloc] init];

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
    });
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {

    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kContactsAccessPermission];
    [[NSUserDefaults standardUserDefaults] synchronize];

    CFErrorRef *error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    NSArray *allPeople = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllPeople(addressBook));

    NSString *name = @"";
    NSString *firstname = @"";
    NSString *lastname = @"";
    for (id person in allPeople)
    {

        firstname = (__bridge NSString *)ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonFirstNameProperty);
        lastname = (__bridge NSString *)ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonLastNameProperty);

        if (!lastname) {
            lastname = @"";
        }

        if (!firstname) {
            firstname = @"";
        }

        name = [NSString stringWithFormat:@"%@ %@",firstname,lastname];
        NSData *data = [name dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        name = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];


        ABMultiValueRef Phone = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty);
        ABMultiValueRef Email = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonEmailProperty);
        NSMutableDictionary *aTemp=[[NSMutableDictionary alloc]init];

        NSMutableArray *arrPhone=[[NSMutableArray alloc]init];
        for (CFIndex i = 0; i < ABMultiValueGetCount(Phone); i++) {
            NSString *phoneNumber = [self removeSpecialCheractersFromPhoneNumber:(__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(Phone, i)];
            [arrPhone addObject:phoneNumber];
        }
        NSMutableArray *arrEmail=[[NSMutableArray alloc]init];
        for (CFIndex i = 0; i < ABMultiValueGetCount(Email); i++) {
            NSString *phoneNumber;
            phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(Email, i);
            [arrEmail addObject:phoneNumber];
        }
        if ([arrPhone count] == 0) {
            [arrPhone addObject:@""];
        }else if ([firstname isEqualToString:@""] && [lastname isEqualToString:@""]) {
            name = [arrPhone firstObject];
        }


        [aTemp setObject:arrPhone forKey:@"phone"];


        if ([arrEmail count] == 0) {
            [arrEmail addObject:@""];
        }
        [aTemp setObject:arrEmail forKey:@"email"];



        if (name) {
            [aTemp setObject:name forKey:@"name"];
        }else{
            [aTemp setObject:@"" forKey:@"name"];
        }


        [arrAddressBook addObject:aTemp];

        CFRelease(Phone);
        CFRelease(Email);
    }
    return arrAddressBook;

}
else {
    // Send an alert telling user to change privacy setting in settings app
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kContactsAccessPermission];
    [[NSUserDefaults standardUserDefaults] synchronize];

}
return nil;
}

The above function uses another function "removeSpecialCheractersFromPhoneNumber". This

//This method removes any type of special character from the phone numbers
-(NSString *) removeSpecialCheractersFromPhoneNumber:(NSString *)phoneNumber
{
NSMutableString *result = [NSMutableString stringWithCapacity:phoneNumber.length];

NSScanner *scanner = [NSScanner scannerWithString:phoneNumber];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

while ([scanner isAtEnd] == NO)
{
    NSString *buffer;
    if ([scanner scanCharactersFromSet:numbers intoString:&buffer])
    {
        [result appendString:buffer];
    }
    else
    {
        [scanner setScanLocation:([scanner scanLocation] + 1)];
    }
}

return result;
}

Hope this will help you

Shubhendu
  • 1,081
  • 8
  • 13
  • My input is an array like so: `("(06)\U00a057\U00a011\U00a042\U00a095")` instead of an nsstring – bdv Jul 29 '14 at 09:55
  • @bdv have you tried the above code??? as whatever you are saving in Addressbook is fetched from ABAddressBookRef and from which i save it into NSString – Shubhendu Jul 29 '14 at 10:32
  • Yes, and I'm sorry, but that doesn't work for me. I need the phoneNumbers grouped per person.. – bdv Jul 29 '14 at 12:12