Using openURL to make a call fails from button in table cell
I have a button in a table view cell and the following action to make a call using the title text开发者_JS百科 of the button
- (IBAction)makeCall:(id)sender
{
UIButton *button = (UIButton *)sender;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",[button titleForState:UIControlStateNormal]]]];
}
This is failing (note: the makeCall action DOES get called, and debugging the title text of the button gives the correct value).
Using the SAME action from a button on the view (i.e. not in a table cell) works.
Anyone know why ?
Thanks.
Are you sure you attached your method to your button?
[yourButton addTarget:self selector:@selector(makeCall:) forControlEvents:UIControlEventTouchUpInside];
You can always debug it by using NSLog
:
NSLog(@"Yep, it does get called, `openURL` just doesn't work ...");
Add the line above below - (IBAction)makeCall:(id)sender ... {
Nevermind, I read you're already sure your method gets called. Add the following line instead:
NSLog(@"Yep, it does get called, `openURL` just doesn't work ...: %@", [sender title]);
If it contains any spaces or special characters (basically all characters except A-Za-z0-9) you may need to perform a little URL-encoding operation on it (or, if possible, just strip those characters away).
精彩评论