0

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;
 }
Dharmesh Porwal
  • 1,406
  • 2
  • 12
  • 21
  • Unrelated, you may want to run this code through the static analyzer ("Analyze" on Xcode's "Product" menu), as I'd be wary about releasing the phone number prior to using it. Frankly, it's much easier to use `__bridge_transfer` with the phone number, and you can just eliminate the `CFRelease(phoneNumberRef)` altogether, and let ARC take care of it (assuming you're using ARC). – Rob Jan 12 '15 at 14:59

1 Answers1

0

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
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Rob thanks for good suggestion but the problem is that I don't found any space's "+" "." "(" ")" in the number extracted. For example Number i got is +917863838383 instead of +91 78-22-222222. – Hussain Chhatriwala Jan 13 '15 at 05:18
  • Hmm. I inferred from all of your code where you were removing spaces and the like that your original number had the punctuation, and I presumed you'd use the above before stripping all of that out. Or are you saying that some of your phone numbers start out with no spaces or the like? In that case, you'd have to look up the country code in a table of country codes most likely. – Rob Jan 13 '15 at 05:58
  • Rob i have no spaces to be removed. I get the space's in the simulator and iPhone 5 device (iOS 8) but not in iPhone 4s(ios7). – Hussain Chhatriwala Jan 13 '15 at 06:16
  • so in iPhone 4s(ios7) the whole number comes in country code. rather then getting separated number and country code. bcuz the number was pure without spaces and not congaing any other non numeric – Hussain Chhatriwala Jan 13 '15 at 06:18