2017-02-20 33 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

感謝您的幫助 - 有時在字符串包含' - '時有效,但最好全部刪除。在我的情況下,字符串在最後一部分包含空白,所以我沒有認出它。謝謝你的幫助! –