簡體   English   中英

電話的NSURL始終為零

[英]NSURL for telephone always nil

這是我的NSURL

let url = NSURL(string: "tel://\(phoneCall)")

始終為nil ,盡管phoneCall是有效的手機號碼字符串,並且我正在實際的iPhone上進行測試。

我試過了

let url = NSURL(string: "tel:\(phoneCall)")

但它仍然nil

let phoneCall = "tel://9000002143";
let url:NSURL = NSURL(string:phoneCall);
    //OR
var url:NSURL = NSURL(string: "tel://9000002143")
if phoneCall == nil || phoneCall.isEmpty 
{
   print("phoneCall is empty or nil.")
}
else
{
  UIApplication.sharedApplication().openURL(url);
}

設置此phoneCall位置?

我在可選類型上也遇到了類似的問題。 此代碼說明了問題:

let user = row.value
let phone = user?.phone
let url = NSURL(string: "tel://\(phone)")
UIApplication.sharedApplication().openURL(url!)

在這種情況下, urlString變量的值為"tel://Optional(\\"11XXXXXXXXX\\")" ,因為user變量可以為nil。

您必須確保user不首先為nil,例如:

guard let user = row.value else {
    return
}
let phone = user.phone
let urlString = "tel://\(phone)"
let url = NSURL(string: urlString)
UIApplication.sharedApplication().openURL(url!)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM