我有一个带有联系人姓名的文本字段,我想获取该电话号码:
ABAddressBookRef adressBook = ABAddressBookCreate();
NSArray *people = (NSArray *)ABAddressBookCopyPeopleWithName(adressBook,
CFStringCreateCopy(kCFAllocatorDefault,
(CFStringRef)recipient));
if((people != nil) && ([people count] == 1)){
ABMultiValueRef person = (ABMultiValueRef)[people objectAtIndex:0];
NSString *phone = (NSString *)ABRecordCopyValue(person,
kABPersonPhoneProperty) ;
NSLog(@"%@", phone);
}
我希望电话号码为字符串,但这给了我更多的信息:
ABMultiValueRef 0x339470 with 1 value(s)
0: _$!<Mobile>!$_ (0x338c50) - 0177 1647788 (0x339450)
如何将数字作为字符串?
答案 0 :(得分:11)
NSMutableArray *phoneNumbers = [[[NSMutableArray alloc] init] autorelease];
ABMultiValueRef multiPhones = ABRecordCopyValue(person,kABPersonPhoneProperty);
for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);++i) {
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
NSString *phoneNumber = (NSString *) phoneNumberRef;
[phoneNumbers addObject:phoneNumber];
}
答案 1 :(得分:2)
kABPersonPhoneProperty是一个多值属性,但您将其视为字符串。您应该获得单独的数字并单独打印。
答案 2 :(得分:1)
CFStringRef phoneRef = ABRecordCopyValue(person, kABPersonPhoneProperty) ;
NSString *phoneNumber = (NSString *) phoneRef;
答案 3 :(得分:1)
对于那些寻找手机提取的人。您可以从文本中提取电话号码,然后将其替换为@&#34;&#34;,例如:
NSString *userBody = @"This is a text with 30612312232 my phone";
if (userBody != nil) {
NSError *error = NULL;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error];
NSArray *matches = [detector matchesInString:userBody options:0 range:NSMakeRange(0, [userBody length])];
if (matches != nil) {
for (NSTextCheckingResult *match in matches) {
if ([match resultType] == NSTextCheckingTypePhoneNumber) {
DbgLog(@"Found phone number %@", [match phoneNumber]);
}
}
}
}
`