Question

In IOS,how to remove +-#*() ,which get from address book. for example:

 NSString *txt = @"+1(510)1234567"
 txt = [NSString stringWithFormat:@"tel://%@", txt];
 [[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:txt]];

it's a invalid number to call. [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlToCall]]; is useless for this type number.

could I have some better options except use [txt stringByReplacingOccurrencesOfString:@"-" withString:@""].... I just wanna make a call.

Thanks for your help.


In fact:we can make a call by openURL: the format of call is not "tel://",It is "tel:". So,what I should do is that:

NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:  
  [[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] 
invertedSet]]     componentsJoinedByString:@""];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", cleanedString]];

That's OK.

———————————————————————————————— @"tel://“ isn't the right url to make a call! we must use @"tel:"

if not some number ,as +1 (510) 3436 which
BOOL bCanCall = [[UIApplication sharedApplication]canOpenURL:urlToCall]; will return False. you can't make a call.

Was it helpful?

Solution

Try this, it may work

-(NSString *) formatIdentificationNumber:(NSString *)string
{
    NSCharacterSet * invalidNumberSet = [NSCharacterSet characterSetWithCharactersInString:@"\n_!@#$%^&*()[]{}'\".,<>:;|\\/?+=\t~` "];

    NSString  * result  = @"";
    NSScanner * scanner = [NSScanner scannerWithString:string];
    NSString  * scannerResult;

    [scanner setCharactersToBeSkipped:nil];

    while (![scanner isAtEnd])
    {
        if([scanner scanUpToCharactersFromSet:invalidNumberSet intoString:&scannerResult])
        {
            result = [result stringByAppendingString:scannerResult];
        }
        else
        {
            if(![scanner isAtEnd])
            {
                [scanner setScanLocation:[scanner scanLocation]+1];
            }
        }
    }

    return result;
}

or see the link

OTHER TIPS

You can remove like this..

NSString *s = @"+1(510)1234567";
NSCharacterSet *unwantedStr = [NSCharacterSet characterSetWithCharactersInString:@"+()"];
s = [[s componentsSeparatedByCharactersInSet: unwantedStr] componentsJoinedByString: @""];
NSLog(@"%@", s); 

Alternativ, some hints here, but you can optimize the code.

- (void)testExample
{
    NSMutableCharacterSet *mvalidCharSet = [[NSMutableCharacterSet alloc] init];
    [mvalidCharSet formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet] ];
    [mvalidCharSet addCharactersInString: @"+"]; 
    NSCharacterSet *validCharSet = [mvalidCharSet copy]; // not mutable -> more efficient
    NSCharacterSet *invalidCharSet = [validCharSet invertedSet];
    NSString *txt = @"+1(510)1234567";
    NSArray * components = [txt componentsSeparatedByCharactersInSet:invalidCharSet];
    NSString * rejoin = [components componentsJoinedByString:@""];
    NSLog(@"%@", rejoin);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top