Skip to main content
 首页 » 编程设计

iphone之地址簿导致崩溃的电话号码(+45)前缀!

2026年05月17日90tintown

我在从iPhone地址簿中获取电话号码时遇到麻烦。

当该数字不包含国家代码前缀(如+45)时没有问题,但是如果确实如此,我的应用程序将崩溃...

这是一个已知的问题?我还没找到任何关于它的东西...

谢谢

编辑:

我得到这样的电话号码:

    -(void)getContact  
    { 
 
        ABPeoplePickerNavigationController *pp = [[ABPeoplePickerNavigationController alloc] init]; 
        pp.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]]; 
        pp.peoplePickerDelegate = self; 
        [self presentModalViewController:pp animated:YES]; 
        [pp release]; 
 
 
    } 
 
    - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { 
        // assigning control back to the main controller 
        [self dismissModalViewControllerAnimated:YES]; 
    } 
 
    - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { 
        return YES; 
    } 
 
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { 
 
            ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property); 
            saveString = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty,identifier); 
            saveString = [saveString stringByReplacingOccurrencesOfString:@" " withString:@""]; 
            nummerTextField.text = saveString; 
        } 

请您参考如下方法:

您如何检索通讯簿对象,一旦检索到该对象,您将如何处理该对象以从中获取编号。我正在使用下面显示的代码执行与您提到的相同的操作,它可以准确地获取编号。

ABRecordRef person = ABAddressBookGetPersonWithRecordID(appDelegate.addressBook, contactId); 
 
ABMultiValueRef multiValue = ABRecordCopyValue(person, kABPersonPhoneProperty); 
 
NSArray *allNumbers = (NSArray *)ABMultiValueCopyArrayOfAllValues(multiValue); 
NSMutableDictionary *filteredNumbers = [NSMutableDictionary new]; 
 
if([allNumbers count] > 0) { 
    for(int contactIndex = 0; contactIndex < [allNumbers count]; contactIndex++) { 
        NSString *contactValue = (NSString *)ABMultiValueCopyLabelAtIndex(multiValue, contactIndex); 
        if(!([contactValue isEqualToString:@"_$!<WorkFAX>!$_"] || [contactValue isEqualToString:@"_$!<HomeFAX>!$_"] || [contactValue isEqualToString:@"_$!<Pager>!$_"])) { 
 
            if([[contactValue substringWithRange:contactLabelCharacterCustom] isEqualToString:@"_$"]) 
                typeOfContact = [contactValue substringWithRange:contactLabelCharacter]; 
            else 
                typeOfContact = [contactValue substringWithRange:(NSRange){0,1}]; 
            NSString *value = (NSString *)ABMultiValueCopyValueAtIndex(multiValue, contactIndex); 
            [filteredNumbers setValue:typeOfContact forKey:value]; 
            [value release]; 
            value = nil; 
        } 
        [contactValue release]; 
        contactValue = nil; 
    } 
} 

我确定它将为您提供帮助。

干杯