问题
I am having trouble getting phone numbers from the iPhone Addressbook.
There is no problem when the number do not contain a country code prefix like +45, but if it does, my app crashes...
Is this a known issue? I haven't been able to find anything about it...
Thanks
EDIT:
I get phonenumber like this:
-(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;
}
回答1:
How are you retrieving your Address book object, and once it is retrieved how do you process it to fetch the number from it.I am using the below shown code to do the same what you have mentioned and it fetches the numbers accurately.
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;
}
}
Im sure it will help you.
Cheers
回答2:
This solved my problem. Hope someone finds it helpful.
ABMultiValueRef multiValue = ABRecordCopyValue(person, property);
NSString *number = (NSString *)ABMultiValueCopyValueAtIndex(multiValue, ABMultiValueGetIndexForIdentifier(multiValue, identifier));
// Error was here: NSString *value = (NSString *)ABMultiValueCopyValueAtIndex(multiValue, contactIndex);
//Copy the number etc before cleaning everything up
saveString = number;
saveString = [saveString stringByReplacingOccurrencesOfString:@" " withString:@""];
nummerTextField.text = saveString;
[number release];
CFRelease(multiValue);
来源:https://stackoverflow.com/questions/4712071/address-book-phone-number-45-prefix-causing-crash