[英]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.