我正在向用户显示地址簿视图,让他们点击联系人并选择一个电话号码。如果他们选择了电话号码,我想将电话号码作为整数,将联系人的姓名作为NSString。
我尝试过使用以下代码:
//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);
此代码在此方法中:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
我正在检查用户是否使用以下代码选择了一个电话号码:
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...
我能够通过printf获取要在控制台中显示的电话号码,但我无法弄清楚如何将其转换为整数以及如何获取联系人姓名,即使所选属性不是一个人的名称。
另外,我正在做的事似乎效率很低。有没有更好的方法来做到这一点?
编辑:如果我不能将它们存储为int,那么字符串就可以了。我只是无法弄清楚如何从该数组转到实际的字符串。如果我将其转换或将其保存为UTF8String,我总会得到一些错误。
答案 0 :(得分:14)
为了有效地获取属性(就读取而言),您可以在回调方法中执行以下操作:
switch( propertyType ) {
case kABMultiStringPropertyType:
// this is the phone number, do something
break;
default:
[self wrongSelection];
break;
}
但是,我不确定你是否真的需要解析它。要从记录中获取电话号码(再次,在回调方法中):
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];
答案 1 :(得分:5)
您无法将电话号码转换为整数。电话号码是字符串。 Apple包含的默认条目为“1-800-MYAPPLE”。
此外,即使电话号码的所有组成部分都是数字,也无法保证世界各地的电话号码实际上都足够小,以适应64位值,一旦您考虑了区号,国家/地区代码,内部扩展等。用户可以自由地在那里放置。
答案 2 :(得分:3)
不使用整数的另一个原因 - 某些国家/地区在电话号码上使用前导零,例如所有英国号码都以零开头(通常写成01234 567890或0123 4567890)!
答案 3 :(得分:2)
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
包含所选的电话号码,personName
包含此人的姓名。如上所述,您不一定必须将字符串转换为数字,因为它可能包含字母字符。但是,您可以编写自己的处理程序将字母字符转换为数字并删除其他所有内容以仅获取数字字符串,然后您可以将其转换为长字符(电话号码会变得很长)。
我怀疑是否需要将电话号码转换为数字值,因为它还可能包含其他必要的字符,如暂停。此外,电话号码表示一串数字,而不是代表一个长号,因此概念数据格式在任何情况下都比Int更多。
答案 4 :(得分:0)
请注意,如果Adressbook-Entry不包含名称或contacdata,则此代码会在“stringWithString”中崩溃。 cfName可能是零!
CFStringRef cfName = ABRecordCopyCompositeName(person);
NSString *personName = [NSString stringWithString:(NSString *)cfName];
CFRelease(cfName);
修正:
NSString *personName = nil;
if ((cfName = ABRecordCopyCompositeName(person)) != nil) {
personName = [NSString stringWithString:(NSString *)cfName];
CFRelease(cfName);
}