[英]Extracting contact variables from profile page (phone #, email address, photo) to iOS Phonebook Contact List
[英]Not getting “-” “.” “ ” while picking up a contact from phonebook from ABPerson
我想按“-”“”“来获取数字。 从电话簿中获取联系人时,这是我的代码。
我的主要动机是,如果存在+,则从数字中提取国家/地区代码。
另外,如果还有其他访问国家/地区代码的方法,也请提出建议。
- (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;
}
我不会倾向于做任何字符串处理的事情,而只是使用正则表达式在字符串的开头查找+
后跟数字,并使用捕获括号仅捕获国家/地区代码:
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.