Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove non numeric characters from phone number in objective-c?

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;
}
like image 835
Fernando Crespo Avatar asked Feb 28 '13 02:02

Fernando Crespo


1 Answers

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.

like image 82
Johan Kool Avatar answered Oct 10 '22 01:10

Johan Kool