Arun
Arun

Reputation: 151

How to dial a number by pressing a call button in an iphone app?

I am using a table view in which each sell has a particular phone number along side a call button on pressing which the number is dialed...For example if i have a phone number in the cell as (425)821-1300 i changed it into the standard format 425-821-1300 by the code as shown below using the string variable phonenumberstring

    phonenumberstring=[phonenumberstring substringFromIndex:1];
    phonenumberstring=[phonenumberstring stringByReplacingOccurrencesOfString:@")" withString:@"-"];

    m_strPhoneNumber = [NSString stringWithFormat:@"tel:%@", self.phonenumberstring];
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:m_strPhoneNumber]];
    NSURL *url = [[NSURL alloc] initWithString:m_strPhoneNumber];//1
    [[UIApplication sharedApplication] openURL:url];//2

the thing is using this when i debug at commented lines 1 & 2 url is shown nill and the call is not dialled

Regards

Arun

Upvotes: 1

Views: 773

Answers (1)

Rog
Rog

Reputation: 17170

What is the value of m_strPhoneNumber and phonenumberstring at that point. Is phonenumberstring being set correctly?

To remove the first bracket why not use stringByReplacingOccurrencesOfString:@"(" withString:@"" etc. it would seem less brittle. Infact, I would suggest stripping out all nonnumeric characters - even the hyphens.

It's probably overkill, but try something like:

m_strPhoneNumber = [NSMutableString stringWithString:@"tel:%@"];
NSArray *numberTokens = [phonenumberstring componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
            for (NSString *token in numberTokens) {
                m_strPhoneNumber = [m_strPhoneNumber stringByAppendingString:token];
            }

As a small style aside, I would suggest phoneNumberString is more readable. I would also recommend using more variables - the optimiser will make sure they don't have an impact and it will make debugging much easier.

Upvotes: 2

Related Questions