在我的專案中,我試圖在 UI 按鈕被置頂時撥打電話。下面是實作,但是當我從模擬器或我的 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)
}
}
}
uj5u.com熱心網友回復:
在您的 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)
uj5u.com熱心網友回復:
你可以試試
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)
}
另外不要忘記tel://在電話號碼前添加
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/436115.html
上一篇:ZEGOExpressSDK錯誤:為iOS模擬器構建,但鏈接和嵌入式框架“xxx.framework”是為iOS iOS模擬器構建的
