IOS
Well, that just CAN be useful to you. I hope so.
In my application, I needed to somehow get the phone number from the contacts. Thus, the problem, as you explained, can be with different characters - * () and with no country codes.
So - I get the contact number using ABPeoplePickerNavigationController and get the true number and the possible country code from the number using the function:
- (void)saveContactPhone:(NSString *) mContactPhone { if(mContactPhone && [mContactPhone length]) { if ([mContactPhone rangeOfString:@"+"].location != NSNotFound)//this means number includes country code. { NSString * mCCodeString = @""; BOOL mFound = FALSE; for(int i = 0; i<[mContactPhone length]; i++) // check number for any obvious country code. { if(mFound == TRUE) { break; } mCCodeString = [mContactPhone substringToIndex:i]; mCCodeString = [[mCCodeString componentsSeparatedByCharactersInSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""]; if([mCCodeString intValue] != 1)//because This is US/CA { for(int j = 0; j<[pickerViewElementArray_ count]; j++) { if([[pickerViewElementArray_ objectAtIndex:j] intValue] == [mCCodeString intValue]) { mFound = TRUE; //we got ourselves final telephone number //and we got country code!! mContactPhone = [mContactPhone substringFromIndex:i]; break; } } } } if(mFound == FALSE)//If no luck finding a match - lets try again, but til index 2. (find if it is +1) { mCCodeString = [mContactPhone substringToIndex:2]; mCCodeString = [[mCCodeString componentsSeparatedByCharactersInSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""]; mContactPhone = [mContactPhone substringFromIndex:1]; for(int i = 0; i<[pickerViewElementArray_ count]; i++) { if([[pickerViewElementArray_ objectAtIndex:i] intValue] == [mCCodeString intValue]) { //we found it!! Its +1!!!! mFound = TRUE; break; } } } } } mContactPhone = [[mContactPhone componentsSeparatedByCharactersInSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""]; }
You also need an array of country code: for example:
NSArray *pickerViewElementArray_ = [NSArray arrayWithObjects: @"93", @"355", @"213", @"1684", @"376", @"244", ....
Hope someone help!
source share