电话号码格式"(06)\ U00a057 \ U00a011 \ U00a042 \ U00a095"从AdressBookAPI记录号码时

时间:2014-07-03 15:58:31

标签: ios objective-c addressbook

我正在阅读AdressBook api中的所有电话号码,在某些情况下,我有这样的格式:"(06)\U00a057\U00a011\U00a042\U00a095"

但是,当检查我自己的地址簿时,它会给我一个常规格式,如0624567899

我该怎么解决?

2 个答案:

答案 0 :(得分:0)

我有同样的问题,在搜索解决方案时看到了你的帖子,然后我想也许我应该解压缩数组而不仅仅是NSLog(@“Phone =%@”,self.array)。因此,您必须解压缩或遍历数组,并且您的值将正确显示:

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

答案 1 :(得分:0)

我要从地址簿中获取电话号码或电子邮件,请尝试此

-(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;
}

上述函数使用另一个函数“removeSpecialCheractersFromPhoneNumber”。此

//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;
}

希望这会对你有所帮助