繁体   English   中英

你如何从地址簿中获得一个人的电话号码?

[英]How do you get a persons phone number from the address book?

我想要做的就是让用户从地址簿中选择一个号码。 我在这个问题中找到了代码:

如何从地址簿联系人处获取电话号码(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”。

这都在picker委托方法中:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{

这只是我的地址簿中的一个联系人,它与Mobile Me同步

编辑:我认为这可能是SDK的一个错误,它发生在我的一些联系人,而不是其他人...

“identifier”参数不包含所触摸记录的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;
}

关于JWD的回答,这是一个更安全的版本,它使用内置常量并选择iphone号码而不是另一个手机号码......

ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* mobile=@"";
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 ;
    }
}
- (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;
}

我用这个问题来构建我自己的解决方案。 我发布的代码不是作为答案,仅仅是为了找到有用的人。 这些是获取房产的简单步骤。 内存管理除外。

我用这个来从ABRecordRef中提取手机号码。“记录”变量是你想要的电话号码的ABRecordRef。 我有“”你可以使用另一个电话标签字符串来查找其他类型的电话号码。

//Get mobile phone number
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(record, kABPersonPhoneProperty);
NSString* mobile=@"";
NSString* mobileLabel;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
    mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
    if([mobileLabel isEqualToString:@"_$!<Mobile>!$_"]) {
          mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
    }
}

您应该能够执行以下操作:

ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
CFRelease(phoneNumbers);
NSString* phoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

当然,这只会让你获得与被选中人相关的第一个(可能)许多数。

您也可以使用此代码中的“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);
    } 

离开Dan的回答:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM