我试图拨打一个不使用特定号码的号码,而是拨打变量中的号码,或者至少告诉它拨打手机中的号码.在变量中调用的这个数字是我使用解析器或从网站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)!) }
Thomas Mülle.. 166
试一试:
if let url = NSURL(string: "tel://\(busPhone)") where UIApplication.sharedApplication().canOpenURL(url) { UIApplication.sharedApplication().openURL(url) }
假设电话号码在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) } }
我们需要检查我们是否在iOS 10或更高版本上,因为:
'openURL'在iOS 10.0中已弃用
好吧,我想我知道问题是什么,但不知道如何解决它.从URL中提取的数字完全按照这个123 456-7890编写,而不是像1234567890那样.如何翻译tel://以读取它将识别的形式的URL? (2认同)
Zorayr.. 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) } } }
你应该可以用来callNumber("7178881234")
打电话.
我在我的应用程序中使用此方法,它工作正常.我希望这对你也有帮助.
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) }
斯威夫特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) } } } }
好的,我得到了帮助并想出来了.此外,我还放了一个漂亮的小警报系统,以防电话号码无效.我的问题是我说的是正确的,但数字有空格和不需要的字符,如("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() } }
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) } } }
你应该可以用来callNumber("7178881234")
打电话.
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) } } }
试一试:
if let url = NSURL(string: "tel://\(busPhone)") where UIApplication.sharedApplication().canOpenURL(url) { UIApplication.sharedApplication().openURL(url) }
假设电话号码在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) } }
我们需要检查我们是否在iOS 10或更高版本上,因为:
'openURL'在iOS 10.0中已弃用
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) } } }
以上答案部分正确,但"tel://"只有一个问题.通话结束后,它将返回主屏幕,而不是我们的应用程序.所以最好使用"telprompt://",它将返回到应用程序.
var url:NSURL = NSURL(string: "telprompt://1234567891")! UIApplication.sharedApplication().openURL(url)