快速拨打电话号码
我试图拨打一个不使用特定号码的电话号码,而是拨打一个在variables中被呼叫的号码,或者至less告诉他们在您的电话号码中input号码。 这个在variables中被调用的数字是我通过使用parsing器或从网站sql获取的数字。 我做了一个button试图调用存储在variables中的电话号码与function,但无济于事。 任何事情都会帮助谢谢!
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)!) }
你试一试:
if let url = NSURL(string: "tel://\(busPhone)") where UIApplication.sharedApplication().canOpenURL(url) { UIApplication.sharedApplication().openURL(url) }
假设电话号码在busPhone
。
NSURL
的init(string:)
返回一个可选的,所以通过使用, if let
我们确保url
是一个NSURL
(而不是由init
返回的NSURL?
)。
对于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) } }
我们需要检查我们是否在iOS 10或更高版本,因为:
在iOS 10.0中不推荐使用“openURL”
iOS 10中的自包含解决schemeSwift 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) } } }
您应该可以使用callNumber("7178881234")
拨打电话。
好吧,我得到了帮助,并找出答案。 此外,我还提供了一个很好的小警报系统,以防电话号码无效。 我的问题是我打电话给它,但数字有空格和不需要的字符,如(“123 456-7890”)。 UIApplication只适用于或接受如果您的号码是(“1234567890”)。 所以你基本上删除空格和无效字符通过使一个新的variables只拉数字。 然后用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() } }
上面的答案是部分正确的,但是“tel://”只有一个问题。 通话结束后,它将返回到主屏幕,而不是我们的应用程序。 所以最好使用“telprompt://”,它会返回到应用程序。
var url:NSURL = NSURL(string: "telprompt://1234567891")! UIApplication.sharedApplication().openURL(url)
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) } } }
我在我的应用程序中使用这种方法,它工作正常。 我希望这可以帮助你。
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) }
在Swift 3中,
if let url = URL(string:"tel://\(phoneNumber)"), UIApplication.shared.canOpenURL(url) { UIApplication.shared.openURL(url) }
这是一个更新@汤姆的答案使用Swift 2.0 注 – 这是我使用的整个CallComposer类。
class CallComposer: NSObject { var editedPhoneNumber = "" func call(phoneNumber: String) -> Bool { if phoneNumber != "" { for i in number.characters { switch (i){ case "0","1","2","3","4","5","6","7","8","9" : editedPhoneNumber = editedPhoneNumber + String(i) default : print("Removed invalid character.") } } let phone = "tel://" + editedPhoneNumber let url = NSURL(string: phone) if let url = url { UIApplication.sharedApplication().openURL(url) } else { print("There was an error") } } else { return false } return true } }
我正在使用数字validation的swift 3解决scheme
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)")!) }
在iOS 10中,openURL()已被弃用。这里是新的语法:
if let url = URL(string: "tel://\(busPhone)") { UIApplication.shared.open(url, options: [:], completionHandler: nil) }
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) } } }
Swift 3.0解决scheme:
let formatedNumber = phone.components(separatedBy: NSCharacterSet.decimalDigits.inverted).joined(separator: "") print("calling \(formatedNumber)") let phoneUrl = "tel://\(formatedNumber)" let url:URL = URL(string: phoneUrl)! UIApplication.shared.openURL(url)
以下是使用Scanner
将电话号码缩减为有效组件的另一种方法。
let number = "+123 456-7890" let scanner = Scanner(string: number) let validCharacters = CharacterSet.decimalDigits let startCharacters = validCharacters.union(CharacterSet(charactersIn: "+#")) var digits: NSString? var validNumber = "" while !scanner.isAtEnd { if scanner.scanLocation == 0 { scanner.scanCharacters(from: startCharacters, into: &digits) } else { scanner.scanCharacters(from: validCharacters, into: &digits) } scanner.scanUpToCharacters(from: validCharacters, into: nil) if let digits = digits as? String { validNumber.append(digits) } } print(validNumber) // +1234567890
Swift 3.0和iOS 10+
UIApplication.shared.openURL(url)
改为UIApplication.shared.open(_ url: URL, options:[:], completionHandler completion: nil)
选项和完成处理程序是可选的,呈现:
UIApplication.shared.open(url)
https://developer.apple.com/reference/uikit/uiapplication/1648685-open
对于Swift 3.1和向后兼容的方法,请执行以下操作:
@IBAction func phoneNumberButtonTouched(_ sender: Any) { if let number = place?.phoneNumber { makeCall(phoneNumber: number) } } func makeCall(phoneNumber: String) { let formattedNumber = phoneNumber.components(separatedBy: NSCharacterSet.decimalDigits.inverted).joined(separator: "") let phoneUrl = "tel://\(formattedNumber)" let url:NSURL = NSURL(string: phoneUrl)! if #available(iOS 10, *) { UIApplication.shared.open(url as URL, options: [:], completionHandler: nil) } else { UIApplication.shared.openURL(url as URL) } }
对于迅速3.0
if let url = URL(string: "tel://\(number)"), UIApplication.shared.canOpenURL(url) { if #available(iOS 10, *) { UIApplication.shared.open(url) } else { UIApplication.shared.openURL(url) } } else { print("Your device doesn't support this feature.") }