Iphone “没有得到”-&引用&引用&引用&引用&引用;从ABPerson的电话簿中提取联系人时
我想在从电话簿中提取联系人时,获得带“-”的号码。这是我的代码。Iphone “没有得到”-&引用&引用&引用&引用&引用;从ABPerson的电话簿中提取联系人时,iphone,abpeoplepickerview,abperson,mobile-country-code,Iphone,Abpeoplepickerview,Abperson,Mobile Country Code,我想在从电话簿中提取联系人时,获得带“-”的号码。这是我的代码。 我的主要目的是在+存在时从数字中提取国家代码。 另外,如果有任何其他方式访问国家代码,请建议我 - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfte
我的主要目的是在+存在时从数字中提取国家代码。
另外,如果有任何其他方式访问国家代码,请建议我
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier;
{
if (property == kABPersonPhoneProperty) {
ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex i = 0; i < ABMultiValueGetCount(multiPhones); i++) {
if(identifier == ABMultiValueGetIdentifierAtIndex (multiPhones, i)) {
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
CFRelease(multiPhones);
NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
CFRelease(phoneNumberRef);
if ([phoneNumber rangeOfString:@"+"].location == NSNotFound) {
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"." withString:@""];
self.lblMobileNumber.text = [NSString stringWithFormat:@"%@", phoneNumber];
} else {
NSArray *PhoneNumberComponents = [phoneNumber componentsSeparatedByString:@" "];
NSString * strCountryCode = PhoneNumberComponents[0] ;
[self.btnCountryCode setTitle:strCountryCode forState:UIControlStateNormal];
phoneNumber= [phoneNumber stringByReplacingOccurrencesOfString:PhoneNumberComponents[0] withString:@""];
NSLog(@"countryCodeSepratedStr%@",phoneNumber);
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"." withString:@""];
self.lblMobileNumber.text = [NSString stringWithFormat:@"%@", phoneNumber];
}
}
}
}
return NO;
}
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker在选择person:(ABRecordRef)person属性:(ABPropertyID)属性标识符:(ABMultiValueIdentifier)标识符后应继续;
{
if(property==kabbersonphoneproperty){
ABMultiValueRef multiPhones=ABRecordCopyValue(person,kABPersonPhoneProperty);
对于(CFIndex i=0;i
我不想做任何字符串操作的事情,只需要使用正则表达式查找+
,然后在字符串开头加上数字,使用捕获括号只获取国家代码:
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^\\s*\\+\\s*(\\d+)[^\\d]*(.*)$" options:0 error:&error];
NSTextCheckingResult *result = [regex firstMatchInString:phoneNumber options:0 range:NSMakeRange(0, [phoneNumber length])];
if (result) {
NSString *countryCode = [phoneNumber substringWithRange:[result rangeAtIndex:1]];
NSString *phoneNumberWithoutCountryCode = [phoneNumber substringWithRange:[result rangeAtIndex:2]];
} else {
// no country code found
}
与此无关,您可能希望通过静态分析器(Xcode的“产品”菜单上的“分析”)运行此代码,因为在使用之前我会小心地释放电话号码。坦白地说,使用电话号码使用
\uu桥接\u传输
要容易得多,您可以完全删除CFRelease(phoneNumberRef)
,让ARC来处理(假设您使用的是ARC)。Rob感谢您的建议,但问题是我在提取的号码中没有找到任何空格“+”。(“”)。例如,我得到的数字是+9178638383,而不是+9178-22-2222222.Hmm。我从你删除空格之类的代码中推断出你原来的数字有标点符号,我猜想你会在去掉这些之前使用上面的代码。或者你是说你的一些电话号码开始时没有空格之类的?在这种情况下,你很可能必须在国家代码表中查找国家代码。Rob我没有要删除的空格。我在模拟器和iPhone5设备(iOS8)中得到了空间,但在iPhone4S(ios7)中没有。所以在iPhone4S(ios7)中,整个数字都是国家代码。而不是将号码和国家代码分开。bcuz这个数字是纯的,没有空格,不包含任何其他非数字