简体   繁体   中英

Not getting “-” “.” “ ” while picking up a contact from phonebook from ABPerson

I want to get number as it is with "-" " " "." while picking up a contact from phone book here's my code .
My main motive is to extract the country code from the number if + is present.
Also please suggest me if there is any other way to access country code.

    - (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;
 }

I wouldn't be inclined do any of that string manipulation stuff, but just use regular expression to look for + followed by number at start of the string, using capturing parentheses to grab just the country code:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM