tags:

views:

272

answers:

1

Hi Guys..

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

+1  A: 

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.

Roger Nolan
Hi RogerThanks for your help..Yes i am getting the correct value of the phone number according to the cell i selected m_strPhoneNumber is in the format @"tel:412-356-8900"..but while debugging the value at url is nill...Please help..just this one issue remaining in my app!!RegardsArun
Arun
Seems weird. I have added code to strip everything except numbers from your phonenumberstring - try that.
Roger Nolan
Hi again...sorry i dint notice the code u sent..yep i tried it...but again in NSURL *url,the url value is shown "nill" while debugging this part of the code..i think unless and untill the url value doesnt indicate as the phone number selected the call wont be initiated...help again?thanks really for responses
Arun
hey Roger...Thanks buddy....i have got the call working now..it was ur code which helped me do it..Thanks Again
Arun
One more problem ...i want to know how could i code that the call has ended and the app could return to my main page rather than exiting the app..
Arun
Roger - Your code also got me past a hump I'd been unable to get over...thanks. Voted your answer up.
Andy
Glad to help. Arun - I missed your last question. I don't think there is a way to do that in 3.0 but you should check the 4.0 docs.
Roger Nolan