如何从地址簿中获得人员电话号码?
我想要做的就是让用户从地址簿中选择一个号码。我发现这个问题的代码:如何从地址簿中获得人员电话号码?
How to get a Phone Number from an Address Book Contact (iphone sdk)
ABMultiValueRef container = ABRecordCopyValue(person, property);
CFStringRef contactData = ABMultiValueCopyValueAtIndex(container, identifier);
CFRelease(container);
NSString *contactString = [NSString stringWithString:(NSString *)contactData];
CFRelease(contactData);
的问题是,在第二行(3.0设备上运行时)我得到以下错误:
客户经理可能找不到帐户标识符的MobileMe:rustyshelf
依次为:
编程接收信号:“EXC_BAD_ACCESS”。
这是所有选择器的委托方法中:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
这仅仅是它同步的与移动我
编辑在我的地址簿中的联系人之一:我觉得这可能是一个错误的SDK,它发生了一些我的联系人,但不是为别人......
你应该能够公正执行以下操作:
ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
CFRelease(phoneNumbers);
NSString* phoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
当然,这只会让你得到与被选人相关的第一批(可能)许多人。
我用它从ABRecordRef中拉出移动号码/“记录”变量是你想要的电话号码的ABRecordRef。如果我有“”,您可以使用另一个电话标签字符串来查找其他类型的电话号码。
//Get mobile phone number
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(record, kABPersonPhoneProperty);
NSString* [email protected]"";
NSString* mobileLabel;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
if([mobileLabel isEqualToString:@"_$!<Mobile>!$_"]) {
mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
}
}
该代码是否无法找到标有'iPhone'标签的手机号码。在澳大利亚,至少似乎自动发生通过MMS接收的联系人。 – rustyshelf 2009-07-14 13:09:49
你是对的,我发布的代码只是搜索标有“_ $!
“标识符”参数不包含所触及记录的CFIndex。我使用的方法来判断哪些电话号码用户选择的是这样的:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
if (property == kABPersonPhoneProperty) {
ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex i = 0; i < ABMultiValueGetCount(multiPhones); i++) {
if(identifier == ABMultiValueGetIdentifierAtIndex (multiPhones, i)) {
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
CFRelease(multiPhones);
NSString *phoneNumber = (NSString *) phoneNumberRef;
CFRelease(phoneNumberRef);
txtPhoneNumber.text = [NSString stringWithFormat:@"%@", phoneNumber];
[phoneNumber release];
}
}
}
[self dismissModalViewControllerAnimated:YES];
return NO;
}
都会响起JWDs答案,这里是使用内置的常量和在其他移动终端号码选择了iphone数量更安全的版本...
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* [email protected]"";
NSString* mobileLabel;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
{
[mobile release] ;
mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
}
else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
{
[mobile release] ;
mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
break ;
}
}
,你也可以在此代码中使用此 “ABMultiValueGetIndexForIdentifier”,如:
ABPropertyType pt = ABPersonGetTypeOfProperty(property);
NSString *phoneNumber;
if ((pt & kABMultiValueMask) == kABMultiValueMask) {
ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property);
CFIndex idx = ABMultiValueGetIndexForIdentifier(phoneProperty, identifier);
phoneNumber = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty,idx);
CFRelease(phoneProperty);
}
从丹的回答都会响起:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
if (property == kABPersonPhoneProperty) { // if tapped is equal to a phone property
CFStringRef cfnumber;
ABMultiValueRef numbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex i = 0; i < ABMultiValueGetCount(numbers); i++) {
if(identifier == ABMultiValueGetIdentifierAtIndex (numbers, i)) { //if tapped number identifier is the same as identifier number tapped
cfnumber = ABMultiValueCopyValueAtIndex(numbers, i); // copy the number to CFSTRING number
}
}
NSString *number = [NSString stringWithFormat:@"%@",cfnumber];
CFRelease(cfnumber);
//do anything you want with the number
}
return NO;
}
不能告诉你,如果这是最好的/正确的方式。但是我用Dan的代码遇到了一些错误,因此我决定在找出它之后再分享它。 仍在学习目标C ..哈哈.. 希望它可以帮助..
问候, Steve0hh
- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue
{
if (property == kABPersonPhoneProperty)
{
ABMultiValueRef numbers = ABRecordCopyValue(person, property);
NSString* targetNumber = (__bridge NSString *) ABMultiValueCopyValueAtIndex(numbers, ABMultiValueGetIndexForIdentifier(numbers, identifierForValue));
NSLog(@"%@", targetNumber);
}
return NO;
}
我用这个问题来构建自己的解决方案。我发布的代码不是为了回答,而仅仅是为了让某些人发现有用。这些是获得财产的简单步骤。内存管理被排除。
感谢您的答案,但我只想找到用户点击的号码。我认为我的代码是正确的,但听起来像是固件3.0中的一个错误阻止了我获取它。上面的代码适用于我的一些联系人,但不适用于其他人。 – rustyshelf 2009-07-14 13:10:38
@Nathan:如果联系人没有电话号码怎么办? – MaikelS 2011-10-19 12:16:45