代码之家  ›  专栏  ›  技术社区  ›  Kisoth Srinathan

iOS Swift呼叫因手机号码无效而崩溃

  •  0
  • Kisoth Srinathan  · 技术社区  · 8 年前

    这是我用来打电话的代码。但由于数字无效,它崩溃了。 11位数字以6开头 通常情况下,10位数可以正常工作。 有人能帮忙吗?

    let myurl=URL(string: "tel://\(selectedEmployeeContact)")
        let isInstalled=UIApplication.shared.canOpenURL(myurl!)
        if(isInstalled)
        {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(myurl!)
            } else {
                UIApplication.shared.openURL(myurl!)
            }
        }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Hussain Shabbir    8 年前

    修改代码以防止崩溃:-

    guard let myurl=URL(string: "tel://\(selectedEmployeeContact)") else {return}
    let isInstalled=UIApplication.shared.canOpenURL(myurl)
    

    如果你的myurl为零,那么它将崩溃,因为它将强制 展开值。

        2
  •  1
  •   gnasher729    8 年前

    回答:感叹号会使它崩溃。故意

    推荐文章