如何从通讯簿联系人(iphone sdk)获取电话号码 [英] How to get a Phone Number from an Address Book Contact (iphone sdk)

查看:107
本文介绍了如何从通讯簿联系人(iphone sdk)获取电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向用户显示地址簿视图,并让他们点击联系人并选择电话号码。如果他们选择一个电话号码,我想得到的电话号码作为整数和联系人的名称作为NSString。



我试着用下面的代码:

  // printf(%s\\\
,[[(NSArray *)ABMultiValueCopyArrayOfAllValues(theProperty)objectAtIndex:identifier] UTF8String]);

// CFArrayRef * arrayString = [[(NSArray *)ABMultiValueCopyArrayOfAllValues(theProperty)objectAtIndex:identifier] UTF8String];
NSArray * arrayString = [(NSArray *)ABMultiValueCopyArrayOfAllValues(theProperty)objectAtIndex:identifier];
printf(%s\\\
,arrayString);

此代码在此方法中:

   - (BOOL)peoplePickerNavigationController :( ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person属性:(ABPropertyID)属性标识符:(ABMultiValueIdentifier)标识符



我正在检查用户是否选择了带有此代码的电话号码:

  if(propertyType == kABStringPropertyType)
{
[self wrongSelection];

}
else if(propertyType == kABIntegerPropertyType)
{
[self wrongSelection];

}
else if(propertyType == kABRealPropertyType)
{
[self wrongSelection];
}
else if(propertyType == kABMultiStringPropertyType)
{
//这是电话号码...

我可以得到的电话号码显示在控制台printf,但我不知道如何将它转换为一个整数,如何获得联系人姓名,即使所选的属性不是人员的姓名。



此外,我的工作似乎效率很低。有没有更好的方法来做到这一点?



编辑:如果我不能存储为一个int,一个字符串会很好。我只是不知道如何从该数组到一个实际的字符串。如果我把它或保存为一个UTF8String我总是得到一些错误。

解决方案

为了有效地获得属性(就读的方式),你可以在回调方法中做这样的事情: / p>

  switch(propertyType){
case kABMultiStringPropertyType:
//这是电话号码, b $ b break;
默认值:
[self wrongSelection];
break;
}



我不确定你是否真的需要解析。要从记录中获取电话号码(再次,在您的回调方法中):

  ABMultiValueRef phoneNumberProperty = ABRecordCopyValue ,kABPersonPhoneProperty); 
NSArray * phoneNumbers =(NSArray *)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
CFRelease(phoneNUmberProperty);

//使用电话号码进行任何操作
NSLog(@电话号码=%@,phoneNumbers);
[phoneNumbers release];


I am showing an addressbook view to the user and letting them click on a contact and select a phone number. If they select a phone number, I want to get the phone number as an integer and the contact's name as an NSString.

I've tried doing it with the following code:

    //printf("%s\n",[[(NSArray *)ABMultiValueCopyArrayOfAllValues(theProperty) objectAtIndex:identifier] UTF8String]);

    //CFArrayRef *arrayString = [[(NSArray *)ABMultiValueCopyArrayOfAllValues(theProperty) objectAtIndex:identifier] UTF8String];
    NSArray *arrayString = [(NSArray *)ABMultiValueCopyArrayOfAllValues(theProperty) objectAtIndex:identifier];
    printf("%s\n", arrayString);

This code is inside this method:

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

And I am checking if the user selected a phone number with this code:

if (propertyType == kABStringPropertyType)   
{
    [self wrongSelection];

} 
else if (propertyType == kABIntegerPropertyType)     
{
    [self wrongSelection];

} 
else if (propertyType == kABRealPropertyType)    
{
    [self wrongSelection];
} 
else if (propertyType == kABMultiStringPropertyType)     
{
     //This is the phone number...

I am able to get the phone number to display in the console with printf, however I can't figure out how to convert it into an integer and how to also get the contacts name even though the property selected is not a person's name.

Also, what I'm doing seems very inefficient. Are there any better ways to do this?

Edit: If I can't store them as an int, a string would be fine. I just can't figure out how to go from that array to an actual string. If I cast it or save it as a UTF8String I always get some error.

解决方案

To get the property efficiently (as far as reading goes), you can do something like this in your callback method:

switch( propertyType ) {
  case kABMultiStringPropertyType:
    // this is the phone number, do something
    break;
  default:
    [self wrongSelection];
    break;
}

I'm not sure you actually even need to parse that, though. To get the phone number from the record you could do (again, inside your callback method):

ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
CFRelease(phoneNUmberProperty);

// Do whatever you want with the phone numbers
NSLog(@"Phone numbers = %@", phoneNumbers);
[phoneNumbers release];

这篇关于如何从通讯簿联系人(iphone sdk)获取电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆