以编程方式拨打电话
如何在 iPhone 上以编程方式拨打电话?我尝试了以下代码但没有发生任何事情:
NSString *phoneNumber = mymobileNO.titleLabel.text;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
How can I make a phone call programmatically on iPhone? I tried the following code but nothing happened:
NSString *phoneNumber = mymobileNO.titleLabel.text;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
迅速
Swift
'openURL:' 已弃用:首先在 iOS 10.0 中弃用 - 请使用 openURL:options:completionHandler: 而
在 Objective-c iOS 10+ 中使用:
'openURL:' is deprecated: first deprecated in iOS 10.0 - Please use openURL:options:completionHandler: instead
in Objective-c iOS 10+ use :
使用开放网址。
要在 swift 5.1 中进行调用,只需使用以下代码:(我已在 Xcode 11 中测试过)
编辑:对于 Xcode 12.4、swift 5.3,只需使用以下代码:
UIApplication.shared.open(NSURL(string: “tel://555-123-1234”)!作为 URL)
确保导入 UIKit,否则它会说在范围内找不到 UIApplication。
Use openurl.
For making a call in swift 5.1, just use the following code: (I have tested it in Xcode 11)
Edit: For Xcode 12.4, swift 5.3, just use the following:
UIApplication.shared.open(NSURL(string: "tel://555-123-1234")! as URL)
Make sure that you import UIKit, or it will say that it cannot find UIApplication in scope.
要返回原始应用程序,您可以使用 telprompt:// 而不是 tel:// -tell 提示符将首先提示用户,但当通话完成后,它将返回您的应用程序:
To go back to original app you can use telprompt:// instead of tel:// - The tell prompt will prompt the user first, but when the call is finished it will go back to your app:
可能 mymobileNO.titleLabel.text 值不包含方案 //
您的代码应如下所示:
ObjectiveC
Swift< /强>
Probably the mymobileNO.titleLabel.text value doesn't include the scheme //
Your code should look like this:
ObjectiveC
Swift
合并 @Cristian Radu 和 @Craig Mellon 的答案以及 @joel.d 的评论,您应该这样做:
这将首先尝试使用“telprompt://”URL,如果失败,它将使用“电话://”网址。如果两者都失败,则您正在尝试在 iPad 或 iPod Touch 上拨打电话。
斯威夫特版本:
Merging the answers of @Cristian Radu and @Craig Mellon, and the comment from @joel.d, you should do:
This will first try to use the "telprompt://" URL, and if that fails, it will use the "tel://" URL. If both fails, you're trying to place a phone call on an iPad or iPod Touch.
Swift Version :
这里的答案非常有效。我只是将克雷格梅隆的答案转换为斯威夫特。如果有人来寻求快速答案,这会对他们有所帮助。
The answers here are perfectly working. I am just converting Craig Mellon answer to Swift. If someone comes looking for swift answer, this will help them.
如果您使用 Xamarin 开发 iOS 应用程序,则以下是与在应用程序中拨打电话等效的 C# 代码:
If you are using Xamarin to develop an iOS application, here is the C# equivalent to make a phone call within your application:
斯威夫特3
Swift 3
在斯威夫特 3.0 中,
In Swift 3.0,
Java RoboVM 等效项:
The Java RoboVM equivalent:
尝试了上面的 Swift 3 选项,但没有成功。我认为如果您要在 Swift 3 上运行 iOS 10+,您需要以下内容:
Swift 3 (iOS 10+):
Tried the Swift 3 option above, but it didnt work. I think you need the following if you are to run against iOS 10+ on Swift 3:
Swift 3 (iOS 10+):