objective-c 如何从地址簿联系人 (iphone sdk) 中获取电话号码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/286207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:53:56  来源:igfitidea点击:

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

iphoneobjective-ccocoa-touch

提问by rksprst

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

编辑:如果我不能将它们存储为 int,那么字符串就可以了。我只是不知道如何从该数组转到实际字符串。如果我将它转换或保存为 UTF8String,我总是会遇到一些错误。

回答by Jason Coco

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

回答by Louis Gerbarg

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 位值,一旦您将区号、国家/地区代码、内部分机因素考虑在内等。用户可以随意放入任意数量的内容。

回答by Dan J

Another reason not to use integers - some countries use leading zeros on phone numbers, e.g. all UK numbers start with a zero (usually written 01234 567890 or 0123 4567890)!

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

回答by Ed Marty

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

contactStringcontains the phone number selected, and personNamecontains the person's name. 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) .

contactString包含所选的电话号码,并personName包含此人的姓名。如上所述,您不一定可以将字符串一般地转换为数字,因为它可能包含字母字符。但是,您可以编写自己的处理程序来将字母字符转换为数字并去掉所有其他内容以仅获取数字字符串,然后您可以将其转换为 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. 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.

不过,我质疑将电话号码转换为数值的必要性,因为它可能还包含其他必要的字符,例如 Pause。此外,无论如何,电话号码代表一串数字比代表一个长数字要多,因此概念数据格式在任何情况下都比 Int 多。

回答by nfriese

Please be aware, that this code crashes in "stringWithString", if the Adressbook-Entry does not contain a name or a contacdata. cfName might be nil!

请注意,如果地址簿条目不包含名称或联系人数据,则此代码会在“stringWithString”中崩溃。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); 
}