简体   繁体   中英

UIApplication not launching to make phone call

I have this code:

NSString *telURL = [NSString stringWithFormat:@"tel:1-%@",((UIButton*) sender).titleLabel.text];
telURL = [telURL stringByReplacingOccurrencesOfString:@"\n" withString:@""];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:telURL]];

But it does not launch the dialer application to make a phone call.

I've checked the string telURL and its contents are : "tel:[REDACTED]", which to my understanding is what it needs to be.

Any ideas?

EDIT: In the event that it matters I've just been running this on the simulator. I won't have a physical device for a few days. (And from the answers below apparently it does matter)

You answered your own question. The tel: URL does NOT work on the iPhone simulator AND the iPod Touch (and iPad, of course). It ONLY works on the iPhone.

That matters. There is no telephone app in the simulator. And trying to open a telephone URL on a device (iPod touch, simulator) that doesn't have the app, just silently fails.

Your formatting is correct though.

First, just checking you are running on hardware that includes a phone (not iPod touch) and not the simulator right? The simulator won't let you test this.

An easy way to get rid of whitespace in your phone number is:

NSString *phoneURLString = [[self.phoneNumber componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString: @""];

Also you should be programmatically checking if the device (or simulator) can handle the url. Simple way to check that is using canOpenURL:

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:yourPhoneNumberStringHere]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM