打电话
Making telephone calls
我使用 Swift 构建了一个简单的 iOS 应用程序,并尝试创建一个允许用户拨打预配置号码的按钮。
下面是我目前使用的代码,从我一直关注的文档来看它似乎是正确的,但 Xcode 显示错误:
Variable used within its own initial value'
显示错误的行是在我尝试调用 UIApplication 时。
@IBAction func contact_number_button(_ sender: AnyObject) {
let contact_number = "XXXXXXXXXXXXXX"
let phoneURL = NSURL(string: "tel://\(contact_number)") {
UIApplication.sharedApplication().openURL(phoneURL)
}
您应该通过以下方式检查 phoneURL
是否可用:
if let phoneURL = NSURL(string: "tel://\(contact_number)") {
UIApplication.sharedApplication().openURL(phoneURL)
}
试着看看Optional Chaining as an Alternative to Forced Unwrapping
第二版有一个好的做法:
@IBAction func contact_number_button(_ sender: AnyObject) {
let contact_number = "XXXXXXXXXXXXXX"
if let phoneURL = NSURL(string: "tel://\(contact_number)") {
UIApplication.sharedApplication().openURL(phoneURL)
}
}
打开可选的版本:
我认为您遇到的问题是张开的大括号,试试这个:
@IBAction func contact_number_button(_ sender: AnyObject) {
let contact_number = "XXXXXXXXXXXXXX"
let phoneURL = NSURL(string: "tel://\(contact_number)")
UIApplication.sharedApplication().openURL(phoneURL!)
}
if let phoneCallURL:NSURL = NSURL(string:"tel://\(contact_number)") {
let application:UIApplication = UIApplication.sharedApplication()
if (application.canOpenURL(phoneCallURL)) {
application.openURL(phoneCallURL);
}
}
非常感谢您的帮助!
这里的所有答案都让我找到了解决方案。我想我确实选择了错误的一周开始自学 Swift 编码并一起编码! XCode 8.0 一直告诉我 iOS10 中的某些命令已被贬低,其中之一是 openURL 命令。下面是现在允许我使用我的 iPhone 6S (iOS 10) 对预配置号码进行工作呼叫的代码。 XCode 仍然告诉我命令 openURL 已被贬值,但它正在运行。如果以下内容不正确或有更好的方法,请随时指正!非常感谢大家!这是我第一次 post,你们的支持太棒了!
@IBAction func contact_number_button(_ sender: AnyObject) {
let contact_number = "XXXXXXXXXXXXXXX"
if let phoneURL = NSURL(string: "tel://\(contact_number)"){
UIApplication.shared.openURL(phoneURL as URL)
}
}
我使用 Swift 构建了一个简单的 iOS 应用程序,并尝试创建一个允许用户拨打预配置号码的按钮。
下面是我目前使用的代码,从我一直关注的文档来看它似乎是正确的,但 Xcode 显示错误:
Variable used within its own initial value'
显示错误的行是在我尝试调用 UIApplication 时。
@IBAction func contact_number_button(_ sender: AnyObject) {
let contact_number = "XXXXXXXXXXXXXX"
let phoneURL = NSURL(string: "tel://\(contact_number)") {
UIApplication.sharedApplication().openURL(phoneURL)
}
您应该通过以下方式检查 phoneURL
是否可用:
if let phoneURL = NSURL(string: "tel://\(contact_number)") {
UIApplication.sharedApplication().openURL(phoneURL)
}
试着看看Optional Chaining as an Alternative to Forced Unwrapping
第二版有一个好的做法:
@IBAction func contact_number_button(_ sender: AnyObject) {
let contact_number = "XXXXXXXXXXXXXX"
if let phoneURL = NSURL(string: "tel://\(contact_number)") {
UIApplication.sharedApplication().openURL(phoneURL)
}
}
打开可选的版本:
我认为您遇到的问题是张开的大括号,试试这个:
@IBAction func contact_number_button(_ sender: AnyObject) {
let contact_number = "XXXXXXXXXXXXXX"
let phoneURL = NSURL(string: "tel://\(contact_number)")
UIApplication.sharedApplication().openURL(phoneURL!)
}
if let phoneCallURL:NSURL = NSURL(string:"tel://\(contact_number)") {
let application:UIApplication = UIApplication.sharedApplication()
if (application.canOpenURL(phoneCallURL)) {
application.openURL(phoneCallURL);
}
}
非常感谢您的帮助!
这里的所有答案都让我找到了解决方案。我想我确实选择了错误的一周开始自学 Swift 编码并一起编码! XCode 8.0 一直告诉我 iOS10 中的某些命令已被贬低,其中之一是 openURL 命令。下面是现在允许我使用我的 iPhone 6S (iOS 10) 对预配置号码进行工作呼叫的代码。 XCode 仍然告诉我命令 openURL 已被贬值,但它正在运行。如果以下内容不正确或有更好的方法,请随时指正!非常感谢大家!这是我第一次 post,你们的支持太棒了!
@IBAction func contact_number_button(_ sender: AnyObject) {
let contact_number = "XXXXXXXXXXXXXXX"
if let phoneURL = NSURL(string: "tel://\(contact_number)"){
UIApplication.shared.openURL(phoneURL as URL)
}
}