从 UIButton 调用
Making a Call From UIButton
在我的项目中,我试图在 UI 按钮处于顶部时进行 phone 调用。下面是实现,但是当我 运行 从模拟器或在我的 iPhone 上它不起作用时。我该如何解决?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ListOfCarsTableViewCell
let carsIndex = carsArray[indexPath.row]
cell.phoneButton.setTitle(carsIndex.dealer.phone.toPhoneNumber(), for: .normal)
return cell
}
@objc func callTapped(_ sender: UIButton) {
let buttonPosition = sender.convert(CGPoint.zero, to: self.tableView)
if let indexPath = tableView.indexPathForRow(at: buttonPosition){
let phoneNumber = carsArray[indexPath.row].dealer.phone
callNumber(phoneNumber: phoneNumber)
}
}
func callNumber(phoneNumber:String) {
if let phoneCallURL = URL(string: "\(phoneNumber)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
application.open(phoneCallURL, options: [:], completionHandler: nil)
}
}
}
在您的 url 字符串中添加“tel://”
func callNumber(phoneNumber:String) {
if let url = URL(string: "tel://\(phoneNumber)") {
UIApplication.shared.open(url, options: [:]) { success in
if success {
print("Making phone call")
} else {
print("Something went wrong while making phone call")
}
}
} else {
print("Failed to make phone call")
}
}
编辑:我发现 callTapped 操作与您的电话按钮没有关联。请在 cellForRowAt 函数中添加以下行。它将完美运行。
cell.phoneButton.addTarget(self, action: #selector(callTapped(_:)), for: .touchUpInside)
你可以试试
cell.phoneButton.tag = indexPath.row
cell.phoneButton.setTitle(carsIndex.dealer.phone.toPhoneNumber(), for: .normal)
cell.phoneButton.addTarget(self, action: #selector(callTapped), for: .touchUpInside)
@objc func callTapped(_ sender: UIButton) {
let phoneNumber = carsArray[sender.tag].dealer.phone
callNumber(phoneNumber: phoneNumber)
}
另外别忘了在phone数字
前加上tel://
在我的项目中,我试图在 UI 按钮处于顶部时进行 phone 调用。下面是实现,但是当我 运行 从模拟器或在我的 iPhone 上它不起作用时。我该如何解决?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ListOfCarsTableViewCell
let carsIndex = carsArray[indexPath.row]
cell.phoneButton.setTitle(carsIndex.dealer.phone.toPhoneNumber(), for: .normal)
return cell
}
@objc func callTapped(_ sender: UIButton) {
let buttonPosition = sender.convert(CGPoint.zero, to: self.tableView)
if let indexPath = tableView.indexPathForRow(at: buttonPosition){
let phoneNumber = carsArray[indexPath.row].dealer.phone
callNumber(phoneNumber: phoneNumber)
}
}
func callNumber(phoneNumber:String) {
if let phoneCallURL = URL(string: "\(phoneNumber)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
application.open(phoneCallURL, options: [:], completionHandler: nil)
}
}
}
在您的 url 字符串中添加“tel://”
func callNumber(phoneNumber:String) {
if let url = URL(string: "tel://\(phoneNumber)") {
UIApplication.shared.open(url, options: [:]) { success in
if success {
print("Making phone call")
} else {
print("Something went wrong while making phone call")
}
}
} else {
print("Failed to make phone call")
}
}
编辑:我发现 callTapped 操作与您的电话按钮没有关联。请在 cellForRowAt 函数中添加以下行。它将完美运行。
cell.phoneButton.addTarget(self, action: #selector(callTapped(_:)), for: .touchUpInside)
你可以试试
cell.phoneButton.tag = indexPath.row
cell.phoneButton.setTitle(carsIndex.dealer.phone.toPhoneNumber(), for: .normal)
cell.phoneButton.addTarget(self, action: #selector(callTapped), for: .touchUpInside)
@objc func callTapped(_ sender: UIButton) {
let phoneNumber = carsArray[sender.tag].dealer.phone
callNumber(phoneNumber: phoneNumber)
}
另外别忘了在phone数字
前加上tel://