15

This is my first attempt to make an ios app.

I'm using people picker to ask the user for a phone number, but when it retrieves with the code below, my NSString *phone apears like (0) 111192222-2222. I'm from Brazil and here the correct mask for mobile phone numbers is (01111) 92222-2222 (the 9 is optional, some numbers have others don't). How to fix this mask? Or remove it entirely?

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    ABMultiValueRef multiValue = ABRecordCopyValue(person, property);
    CFIndex index = ABMultiValueGetIndexForIdentifier(multiValue, identifier);
    NSString *phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(multiValue, index);
    return NO;
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Fernando Crespo
  • 484
  • 6
  • 17
  • Could you be more specific? If the NSString is `(0) 111192222-2222` what would you like it to be? – matt Feb 28 '13 at 02:21
  • To remove the characters entirely it's probably simplest to simply iterate through them, copying them from string A to string B, and remove the ones you don't want. Yes, you can probably use REs and whatnot, but why make the code more obscure? – Hot Licks Feb 28 '13 at 02:48

5 Answers5

42

See this answer: https://stackoverflow.com/a/6323208/60488

Basically:

NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];

For your case you may want to remove the characters "-", "(" and ")" from the character set.

Community
  • 1
  • 1
Johan Kool
  • 15,637
  • 8
  • 64
  • 81
  • Thank you, this is so more cleaner and quickier than what I was doing with `stringByReplacingOccurrencesOfString` and the others answers. – Fernando Crespo Feb 28 '13 at 13:32
  • 4
    Just to add some info. After studying that code I would actually do `[NSCharacterSet decimalDigitCharacterSet]` because its in the API and fits my needs! – Fernando Crespo Mar 05 '13 at 21:39
  • Slick, I like the use of the invertedSet. – Ralph Caraveo Nov 12 '13 at 22:46
  • Very useful. I turned this into `+(NSString *)filterString:(NSString *)subject filter:(NSString *)filter{ NSString *cleanedString = [[subject componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:filter] invertedSet]] componentsJoinedByString:@""]; return cleanedString; }` – chiliNUT Apr 21 '16 at 17:30
2

You can use few methods of NSString and NSMutableString as :

NSString *phone=@"(0) 111192222-2222";
//I'm from Brazil and here the correct mask for mobile phone numbers is (01111) 92222-2222
NSMutableString *editPhone=[NSMutableString stringWithString:[phone stringByReplacingOccurrencesOfString:@")" withString:@""]];
editPhone=[NSMutableString stringWithString:[editPhone stringByReplacingOccurrencesOfString:@" " withString:@""]];

[editPhone insertString:@") " atIndex:6];


NSLog(@"%@",editPhone);//(01111) 92222-2222
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • 2
    This seems to be a very fragile approach. Prone to crashes if the phone number given is shorter than expected. – Johan Kool Feb 28 '13 at 03:49
  • @JohanKool: Agree with you, But as per OP's question this is the solution. However if he asks then the solution can be altered with all sorts of valicdaions – Anoop Vaidya Feb 28 '13 at 07:47
  • In the question I've mentioned that **the 9 is optional, some numbers have others don't** so it would crash with certain phone numbers, but thank you for the solution. It DOES resolve that case. – Fernando Crespo Feb 28 '13 at 13:40
  • @FernandoCrespo: Not an issue. you can count number of characters in the `phone`. If it is lesser then specified, definitely 9 is missing, you can go on to add a nine and then follow the logic as in answer. – Anoop Vaidya Feb 28 '13 at 15:00
2

I think there are ways to solve this:

  1. Using NSRegularExpression to remove anything but numbers. You can see here or here to know how to validate phone number.
  2. Write your own scanner to remove characters you don't need. Remove blanks or remove all but numbers.
  3. Use the UITextFieldDelegate, write the textField:shouldChangeCharactersInRange:replacementString: method, check the replacement string if it is in the range of 0-9.

Hope helps.

Community
  • 1
  • 1
Jason Lee
  • 3,200
  • 1
  • 34
  • 71
  • Give you a point because of the nice aditions to my knowledge and because it answers my question, but the other answer below I find it more cleaner and quickier way to achieve what I was going to. – Fernando Crespo Feb 28 '13 at 13:37
1

I would use Regular expression to validate the phone number instead of killing myself to make a custom keyboard, which functions can be changed by iOS updates. So, allow all characters and validate inside the code.

0

SWIFT 5.0 solution

let purePhoneNumber = phoneNumber.replacingOccurrences(of: "[^0-9]", 
                                                       with: "", 
                                                       options: .regularExpression)

If you want to leave + sign in string, you can use regexp [^0-9+].

Argus
  • 2,241
  • 1
  • 22
  • 27