如何从iOS应用程序拨打电话号码?

我试图从ios应用程序调用电话号码使用:它不工作,虽然方法被调用:

-(IBAction)callPhone:(id)sender { NSString *phoneCallNum = [NSString stringWithFormat:@"tel://%@",listingPhoneNumber ]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneCallNum]]; NSLog(@"phone btn touch %@", phoneCallNum); } 

NSLog输出:手机btn touch电话:// + 39 0668806972

你的代码是正确的。 你检查了真实的设备吗? 这个function在模拟器中不起作用。

试试这也是,

 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版本**

 if let url = URL(string: "telprompt:\(phoneNumber)") { if UIApplication.shared.canOpenURL(url) { UIApplication.shared.openURL(url) } } 

电话不能在模拟器/ iPod / iPad上运行,您需要在运行SIM卡的iPhone上运行应用程序。

另外调用电话应用程序的URLscheme是tel:<phone_number> 。 请参阅Apple文档。

理想情况下,您应该检查设备是否有电话模块,然后执行openURL:调用。 使用此代码执行检查,

 if([[UIApplication sharedApplication] canOpenURL:callUrl]) { [[UIApplication sharedApplication] openURL:callUrl]; } else { //Show error message to user, etc. } 

使用以下方法拨打电话:

  NSString *phoneNumber = [@"tel://" stringByAppendingString:number]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]]; 

你可以尝试下面。

 NSString *phoneNumber = [@"tel://" stringByAppendingString:Number]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]]; 

希望它可以帮助你。

  NSString *phoneNumber = [@"tel://" stringByAppendingString:@"9414481799"]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]]; 

这只会在设备上运行。