Tho*_*nez 74 phone-call phone-number ios swift
我试图拨打一个不使用特定号码的号码,而是拨打变量中的号码,或者至少告诉它拨打手机中的号码.在变量中调用的这个数字是我使用解析器或从网站sql中获取的数字.我做了一个按钮试图通过功能调用存储在变量中的电话号码,但无济于事.什么都有帮助谢谢!
func callSellerPressed (sender: UIButton!){
//(This is calls a specific number)UIApplication.sharedApplication().openURL(NSURL(string: "tel://######")!)
// This is the code I'm using but its not working
UIApplication.sharedApplication().openURL(NSURL(scheme: NSString(), host: "tel://", path: busPhone)!)
}
Run Code Online (Sandbox Code Playgroud)
Tho*_*ler 166
试一试:
if let url = NSURL(string: "tel://\(busPhone)") where UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.sharedApplication().openURL(url)
}
Run Code Online (Sandbox Code Playgroud)
假设电话号码在busPhone
.
NSURL
的init(string:)
返回一个可选的,所以通过使用if let
我们确保url
是一个NSURL
(而不是NSURL?
通过返回的init
).
对于Swift 3:
if let url = URL(string: "tel://\(busPhone)"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
Run Code Online (Sandbox Code Playgroud)
我们需要检查我们是否在iOS 10或更高版本上,因为:
'openURL'在iOS 10.0中已弃用
Zor*_*ayr 63
iOS 10中的自包含解决方案,Swift 3:
private func callNumber(phoneNumber:String) {
if let phoneCallURL = URL(string: "tel://\(phoneNumber)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
application.open(phoneCallURL, options: [:], completionHandler: nil)
}
}
}
Run Code Online (Sandbox Code Playgroud)
你应该可以用来callNumber("7178881234")
打电话.
Tej*_*eja 15
斯威夫特4,
private func callNumber(phoneNumber:String) {
if let phoneCallURL = URL(string: "telprompt://\(phoneNumber)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
if #available(iOS 10.0, *) {
application.open(phoneCallURL, options: [:], completionHandler: nil)
} else {
// Fallback on earlier versions
application.openURL(phoneCallURL as URL)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
gra*_*spo 13
斯威夫特 5:iOS >= 10.0
这个解决方案是零保存。
仅适用于物理设备。
private func callNumber(phoneNumber: String) {
guard let url = URL(string: "telprompt://\(phoneNumber)"),
UIApplication.shared.canOpenURL(url) else {
return
}
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Run Code Online (Sandbox Code Playgroud)
小智 11
Swift 3.0和ios 10或更早版本
func phone(phoneNum: String) {
if let url = URL(string: "tel://\(phoneNum)") {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url as URL)
}
}
}
Run Code Online (Sandbox Code Playgroud)
好的,我得到了帮助并想出来了.此外,我还放了一个漂亮的小警报系统,以防电话号码无效.我的问题是我说的是正确的,但数字有空格和不需要的字符,如("123 456-7890").如果您的号码是("1234567890"),则UIApplication仅适用或接受.因此,您基本上通过创建一个新变量来仅删除数字来删除空格和无效字符.然后使用UIApplication调用这些数字.
func callSellerPressed (sender: UIButton!){
var newPhone = ""
for (var i = 0; i < countElements(busPhone); i++){
var current:Int = i
switch (busPhone[i]){
case "0","1","2","3","4","5","6","7","8","9" : newPhone = newPhone + String(busPhone[i])
default : println("Removed invalid character.")
}
}
if (busPhone.utf16Count > 1){
UIApplication.sharedApplication().openURL(NSURL(string: "tel://" + newPhone)!)
}
else{
let alert = UIAlertView()
alert.title = "Sorry!"
alert.message = "Phone number is not available for this business"
alert.addButtonWithTitle("Ok")
alert.show()
}
}
Run Code Online (Sandbox Code Playgroud)
以上答案部分正确,但"tel://"只有一个问题.通话结束后,它将返回主屏幕,而不是我们的应用程序.所以最好使用"telprompt://",它将返回到应用程序.
var url:NSURL = NSURL(string: "telprompt://1234567891")!
UIApplication.sharedApplication().openURL(url)
Run Code Online (Sandbox Code Playgroud)
小智 7
我在我的应用程序中使用此方法,它工作正常.我希望这对你也有帮助.
func makeCall(phone: String) {
let formatedNumber = phone.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
let phoneUrl = "tel://\(formatedNumber)"
let url:NSURL = NSURL(string: phoneUrl)!
UIApplication.sharedApplication().openURL(url)
}
Run Code Online (Sandbox Code Playgroud)
Swift 3,iOS 10
func call(phoneNumber:String) {
let cleanPhoneNumber = phoneNumber.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
let urlString:String = "tel://\(cleanPhoneNumber)"
if let phoneCallURL = URL(string: urlString) {
if (UIApplication.shared.canOpenURL(phoneCallURL)) {
UIApplication.shared.open(phoneCallURL, options: [:], completionHandler: nil)
}
}
}
Run Code Online (Sandbox Code Playgroud)
许多其他答案不适用于 Swift 5。以下是 Swift 5 的代码更新:
let formattedNumber = phoneNumberVariable.components(separatedBy: NSCharacterSet.decimalDigits.inverted).joined(separator: "")
if let url = NSURL(string: ("tel:" + (formattedNumber)!)) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url as URL)
}
}
Run Code Online (Sandbox Code Playgroud)
附:
在斯威夫特 3 中,
if let url = URL(string:"tel://\(phoneNumber)"), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
}
Run Code Online (Sandbox Code Playgroud)
我正在使用带有数字验证的 swift 3 解决方案
var validPhoneNumber = ""
phoneNumber.characters.forEach {(character) in
switch character {
case "0"..."9":
validPhoneNumber.characters.append(character)
default:
break
}
}
if UIApplication.shared.canOpenURL(URL(string: "tel://\(validNumber)")!){
UIApplication.shared.openURL(URL(string: "tel://\(validNumber)")!)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
99672 次 |
最近记录: |