ios 如何从ios应用程序拨打电话号码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18914156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a phone number from ios app?
提问by user2588945
I'm trying to call a phone number from ios app using: It's not working, although the method gets called:
我正在尝试使用以下方法从 ios 应用程序拨打电话号码:它不起作用,尽管该方法被调用:
-(IBAction)callPhone:(id)sender {
NSString *phoneCallNum = [NSString stringWithFormat:@"tel://%@",listingPhoneNumber ];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneCallNum]];
NSLog(@"phone btn touch %@", phoneCallNum);
}
NSLog
output: phone btn touch tel://+39 0668806972
NSLog
输出:电话 btn touch tel://+39 0668806972
回答by Yasodha
your code is correct. did you check in real device. this function will not work in simulator.
你的代码是正确的。你在真机上签到了吗?此功能在模拟器中不起作用。
try this also,
也试试这个,
NSString *phNo = @"+919876543210";
NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@",phNo]];
if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
[[UIApplication sharedApplication] openURL:phoneUrl];
} else
{
calert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Call facility is not available!!!" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[calert show];
}
** Swift 3 version**
** Swift 3 版本**
if let url = URL(string: "telprompt:\(phoneNumber)") {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
}
}
回答by Amar
Telephony does not work on simulators/iPod/iPad, you will require to run the app on an iPhone with active sim card.
电话在模拟器/iPod/iPad 上不起作用,您需要在带有活动 SIM 卡的 iPhone 上运行该应用程序。
Also the URL scheme to invoke the telephony application is tel:<phone_number>
. Refer Apple docs.
调用电话应用程序的 URL 方案也是tel:<phone_number>
. 请参阅Apple 文档。
Ideally, you should check if the device is having the telephony module and then perform the openURL:
call. Use this code to perform the check,
理想情况下,您应该检查设备是否具有电话模块,然后执行openURL:
呼叫。使用此代码执行检查,
if([[UIApplication sharedApplication] canOpenURL:callUrl]) {
[[UIApplication sharedApplication] openURL:callUrl];
}
else {
//Show error message to user, etc.
}
回答by Rigel Networks
Use following method to make call:
使用以下方法拨打电话:
NSString *phoneNumber = [@"tel://" stringByAppendingString:number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
回答by kshitij godara
NSString *phoneNumber = [@"tel://" stringByAppendingString:@"9414481799"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
This will only run on device.
这只会在设备上运行。
回答by Manthan
You can try like below.
您可以尝试如下。
NSString *phoneNumber = [@"tel://" stringByAppendingString:Number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Hope it helps you.
希望对你有帮助。