2017-02-20 51 views
0

我编写了一些代码来启动来自String的电话呼叫,如附加的图像。 [功能:phoneCallInitiation] [1]ios 10在某些号码上没有启动Swift电话

某些电话号码可以很好地发起与格式,如02-123-1234,但一些电话号码031-123-1234不能启动,但我可以看到phoneNumberString是运作良好。

你能猜出任何理由来这样工作吗?

func callButtonPressed() { 
    let alertViewController = UIAlertController(title: NSLocalizedString("Call", comment: ""), message: "\(storeToDisplay.phoneNumber!)", preferredStyle: .alert) 
    alertViewController.addAction(UIAlertAction(title: "Call", style: .default, handler: { (alertAction) -> Void in 
     let phoneNumberString = "tel://\(self.storeToDisplay.phoneNumber!)" 
     print("This is phonenumber: \(phoneNumberString)") 
     if let url = URL(string: phoneNumberString) { 
      UIApplication.shared.open(url, options: [:], completionHandler: nil) 
     } else { 
      print("WHAT Happenes") 
     } 
    })) 
    alertViewController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (alertAction) -> Void in 
     alertViewController.dismiss(animated: true, completion: nil) 
    })) 
    present(alertViewController, animated: true, completion: nil) 
} 
+0

删除'-',并尝试一次 –

+0

我也试着删除' - '在电话号码中间,但结果仍然是一样的。无法拨打电话 –

回答

0

如果您使用的是模拟你的应用程序,那么你不会找到任何东西发生。如果您正在使用模拟器,请尝试在实际设备上运行。

试试这个

let replaced = self.storeToDisplay.phoneNumber!.replacingOccurrences(of: "-", with: "") 
    let phoneNumberString = "tel://\(replaced)" 

,并添加

if let url = URL(string: "telprompt://\(replaced)") { 
       UIApplication.shared.open(url, options: [:], completionHandler: nil) 
      } 

否则,如果你想直接调用

if let url = URL(string: "tel://\(replaced)") { 
       UIApplication.shared.open(url, options: [:], completionHandler: nil) 
      } 
+0

感谢您的帮助 - 有时在字符串包含' - '时有效,但最好全部删除。在我的情况下,字符串在最后一部分包含空白,所以我没有认出它。谢谢你的帮助! –