简体   繁体   English

如何从地址簿联系人处获取电话号码(iphone sdk)

[英]How to get a Phone Number from an Address Book Contact (iphone sdk)

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. 如果他们选择了电话号码,我想将电话号码作为整数,将联系人的姓名作为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. 我能够使用printf在控制台中显示电话号码,但是我无法弄清楚如何将其转换为整数以及如何获取联系人姓名,即使所选属性不是人名。

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. 编辑:如果我不能将它们存储为int,则字符串就可以了。 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. 如果我将其转换或将其保存为UTF8String,我总会得到一些错误。

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];

You can't convert the phone number into an integer. 您无法将电话号码转换为整数。 Phone numbers are strings. 电话号码是字符串。 The default entry Apple includes for itself has the number "1-800-MYAPPLE". Apple包含的默认条目为“1-800-MYAPPLE”。

Also, even if all components of a phone number are digits, there is no guarantee that phone numbers in all parts of the world are actually small enough to fit inside a 64 bit value, once you factor in area codes, country codes, internal extensions, etc. Users are free to put as much as they want in there. 此外,即使电话号码的所有组件都是数字,也无法保证世界各地的电话号码实际上都足够小,以适应64位值,一旦您考虑区号,国家代码,内部扩展等等。用户可以自由地在那里放置。

不使用整数的另一个原因 - 一些国家在电话号码上使用前导零,例如所有英国号码都以零开头(通常写成01234 567890或0123 4567890)!

CFStringRef cfName = ABRecordCopyCompositeName(person);
NSString *personName = [NSString stringWithString:(NSString *)cfName];
CFRelease(cfName);

ABMultiValueRef container = ABRecordCopyValue(person, property);
CFStringRef contactData = ABMultiValueCopyValueAtIndex(container, identifier);
CFRelease(container);
NSString *contactString = [NSString stringWithString:(NSString *)contactData];
CFRelease(contactData);

contactString contains the phone number selected, and personName contains the person's name. contactString包含所选的电话号码, personName包含该人的姓名。 As stated above, you can't necessarily convert the string to numbers generically, as it may contain alphabetic characters. 如上所述,您不一定必须将字符串转换为数字,因为它可能包含字母字符。 However, you could write your own handler to convert alphabetic characters to numbers and strip out everything else to get a numeric string only, which you could then convert to a long (phone numbers get pretty long) . 但是,您可以编写自己的处理程序将字母字符转换为数字并删除其他所有内容以仅获取数字字符串,然后您可以将其转换为长字符(电话号码变得非常长)。

I question the need to convert a phone number to a numeric value, though, since it may also contain other necessary characters like Pause. 我怀疑是否需要将电话号码转换为数字值,因为它还可能包含其他必要的字符,如Pause。 Also, a phone number represents a string of digits more than it represents one long number anyway, so the conceptual data format is more String than Int in any case. 此外,电话号码表示一串数字,而不是代表一个长号码,因此概念数据格式在任何情况下都比Int更多。

Please be aware, that this code crashes in "stringWithString", if the Adressbook-Entry does not contain a name or a contacdata. 请注意,如果Adressbook-Entry不包含名称或contacdata,则此代码会在“stringWithString”中崩溃。 cfName might be nil! cfName可能是零!

CFStringRef cfName = ABRecordCopyCompositeName(person);
NSString *personName = [NSString stringWithString:(NSString *)cfName];
CFRelease(cfName); 

fix: 固定:

NSString *personName = nil;
if ((cfName = ABRecordCopyCompositeName(person)) != nil) {
     personName = [NSString stringWithString:(NSString *)cfName];
     CFRelease(cfName); 
}

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

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