2014-12-02 260 views
40

我正在嘗試撥打一個不使用特定號碼的號碼,而是撥打一個在變量中被呼叫的號碼,或者至少告訴它在您的手機中撥打號碼。這個在變量中被調用的數字是我通過使用解析器或從網站sql獲取的數字。我做了一個按鈕,試圖用函數調用存儲在變量中的電話號碼,但無濟於事。任何事情都會有幫助謝謝!在swift中撥打電話號碼

func callSellerPressed (sender: UIButton!){ 
//(This is calls a specific number)UIApplication.sharedApplication().openURL(NSURL(string: "tel://######")!) 

// This is the code I'm using but its not working  
UIApplication.sharedApplication().openURL(NSURL(scheme: NSString(), host: "tel://", path: busPhone)!) 

     } 

回答

112

試試看:

if let url = NSURL(string: "tel://\(busPhone)") where UIApplication.sharedApplication().canOpenURL(url) { 
    UIApplication.sharedApplication().openURL(url) 
} 

假設的電話號碼是busPhone

NSURLinit(string:)返回一個可選的,所以通過使用if let我們確保urlNSURL(而不是NSURL?init返回)。


對於斯威夫特3:

if let url = URL(string: "tel://\(busPhone)"), UIApplication.shared.canOpenURL(url) { 
    if #available(iOS 10, *) { 
     UIApplication.shared.open(url) 
    } else { 
     UIApplication.shared.openURL(url) 
    } 
} 

我們需要檢查我們是否是iOS上的10或更高版本,因爲:

'openURL' was deprecated in iOS 10.0

+0

這給了我一個未解決的標識符'url'錯誤 - 指向.openURL(url)感謝您的幫助! – Tom 2014-12-02 22:22:05

+0

nvm我錯過了一封信。好吧,所以當我按下按鈕什麼都沒有發生。這裏是更多的信息---------- func parserDidEndDocument(解析器:NSXMLParser!){ println(busPhone) drawUpdatedView() } – Tom 2014-12-02 22:30:52

+0

你在手機上測試這個嗎?我懷疑打開tel:// URL只能在實際的iPhone上使用,而不能在模擬器中使用,而不能在iPod或iPad上使用。 – 2014-12-03 02:49:42

8

好我得到的幫助和理解了它。此外,爲了防止電話號碼無效,我還提供了一個很好的小警報系統。我的問題是我打電話給它的權利,但數字有空格和不需要的字符,如(「123 456-7890」)。如果您的號碼是(「1234567890」),則UIApplication僅適用或接受。所以你基本上通過創建一個新的變量來只取出數字來刪除空格和無效字符。然後用UIApplication調用這些數字。

func callSellerPressed (sender: UIButton!){ 
     var newPhone = "" 

     for (var i = 0; i < countElements(busPhone); i++){ 

      var current:Int = i 
      switch (busPhone[i]){ 
       case "0","1","2","3","4","5","6","7","8","9" : newPhone = newPhone + String(busPhone[i]) 
       default : println("Removed invalid character.") 
      } 
     } 

     if (busPhone.utf16Count > 1){ 

     UIApplication.sharedApplication().openURL(NSURL(string: "tel://" + newPhone)!) 
     } 
     else{ 
      let alert = UIAlertView() 
      alert.title = "Sorry!" 
      alert.message = "Phone number is not available for this business" 
      alert.addButtonWithTitle("Ok") 
       alert.show() 
     } 
     } 
+6

您還應該允許我想的'+'號。 – florian 2015-05-01 17:13:58

+1

另外#是在提出請求時使用的字符之一:) – 2015-12-18 11:20:36

+0

你是上帝發送的 – 2016-04-27 04:52:44

47

在iOS的10自載的解決方案,斯威夫特3

private func callNumber(phoneNumber:String) { 

    if let phoneCallURL = URL(string: "tel://\(phoneNumber)") { 

    let application:UIApplication = UIApplication.shared 
    if (application.canOpenURL(phoneCallURL)) { 
     application.open(phoneCallURL, options: [:], completionHandler: nil) 
    } 
    } 
} 

你應該能夠使用callNumber("7178881234")撥打電話。

+0

這個工作與xcode中的模擬器嗎? – Pavlos 2017-05-03 15:51:00

+0

它會給出如下錯誤; 「該應用程序不允許查詢計劃電話」爲SWIFT 3,這些工作是否在模擬器上? – 2017-08-08 07:00:17

+0

該代碼在模擬器中不起作用。如果你想檢查它使用一個設備。 – Ramakrishna 2017-08-28 08:57:36

3

這是@Tom的回答使用Swift 2.0的更新 注意 - 這是我使用的整個CallComposer類。

class CallComposer: NSObject { 

var editedPhoneNumber = "" 

func call(phoneNumber: String) -> Bool { 

    if phoneNumber != "" { 

     for i in number.characters { 

      switch (i){ 
       case "0","1","2","3","4","5","6","7","8","9" : editedPhoneNumber = editedPhoneNumber + String(i) 
       default : print("Removed invalid character.") 
      } 
     } 

    let phone = "tel://" + editedPhoneNumber 
     let url = NSURL(string: phone) 
     if let url = url { 
      UIApplication.sharedApplication().openURL(url) 
     } else { 
      print("There was an error") 
     } 
    } else { 
     return false 
    } 

    return true 
} 
} 
5

我在我的應用程序中使用這種方法,它工作正常。我希望這可以幫助你。

func makeCall(phone: String) { 
    let formatedNumber = phone.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("") 
    let phoneUrl = "tel://\(formatedNumber)" 
    let url:NSURL = NSURL(string: phoneUrl)! 
    UIApplication.sharedApplication().openURL(url) 
} 
0

夫特3.0溶液:

let formatedNumber = phone.components(separatedBy: NSCharacterSet.decimalDigits.inverted).joined(separator: "") 
print("calling \(formatedNumber)") 
let phoneUrl = "tel://\(formatedNumber)" 
let url:URL = URL(string: phoneUrl)! 
UIApplication.shared.openURL(url) 
+0

它也刪除'+'和'#'字符 – 2017-11-07 13:49:39

4

在夫特3,

if let url = URL(string:"tel://\(phoneNumber)"), UIApplication.shared.canOpenURL(url) { 
    UIApplication.shared.openURL(url) 
} 
7

上述答案是部分正確,但與 「電話://」 只存在一個問題。通話結束後,它將返回到主屏幕,而不是我們的應用程序。所以最好使用「telprompt://」,它會返回到應用程序。

var url:NSURL = NSURL(string: "telprompt://1234567891")! 
UIApplication.sharedApplication().openURL(url) 
+0

顯然這會讓你的應用程序[拒絕](https://stackoverflow.com/questions/29477108/replacing-tel-or-telprompt-to-call) – 2017-08-14 10:25:06

2

的OpenURL()已在IOS 10.這裏被棄用是新的語法:

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

其不貶值。語法的改變是因爲你使用的是swift 3. – Hammadzafar 2016-11-15 07:42:54

+0

@Hammadzafar,openURl(url)方法真的被棄用了。它的新版本有不同的簽名,並且已經重新命名爲打開(url:options:completionHandler) – mimic 2016-12-15 17:50:45

+0

這是因爲swift 2.3很快就會被折舊 – Hammadzafar 2016-12-19 06:31:46

7

夫特3.0和IOS 10或以上

func phone(phoneNum: String) { 
    if let url = URL(string: "tel://\(phoneNum)") { 
     if #available(iOS 10, *) { 
      UIApplication.shared.open(url, options: [:], completionHandler: nil) 
     } else { 
      UIApplication.shared.openURL(url as URL) 
     } 
    } 
} 
0

這裏的減少的另一種方式使用Scanner的有效元件的電話號碼...

let number = "+123 456-7890" 

let scanner = Scanner(string: number) 

let validCharacters = CharacterSet.decimalDigits 
let startCharacters = validCharacters.union(CharacterSet(charactersIn: "+#")) 

var digits: NSString? 
var validNumber = "" 
while !scanner.isAtEnd { 
    if scanner.scanLocation == 0 { 
     scanner.scanCharacters(from: startCharacters, into: &digits) 
    } else { 
     scanner.scanCharacters(from: validCharacters, into: &digits) 
    } 

    scanner.scanUpToCharacters(from: validCharacters, into: nil) 
    if let digits = digits as? String { 
     validNumber.append(digits) 
    } 
} 

print(validNumber) 

// +1234567890 
3

我使用SWIFT 3溶液用數字驗證

var validPhoneNumber = "" 
    phoneNumber.characters.forEach {(character) in 
     switch character { 
     case "0"..."9": 
      validPhoneNumber.characters.append(character) 
     default: 
      break 
     } 
    } 

    if UIApplication.shared.canOpenURL(URL(string: "tel://\(validNumber)")!){ 
     UIApplication.shared.openURL(URL(string: "tel://\(validNumber)")!) 
    } 
2

夫特3,iOS裝置10

func call(phoneNumber:String) { 
     let cleanPhoneNumber = phoneNumber.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "") 
     let urlString:String = "tel://\(cleanPhoneNumber)" 
     if let phoneCallURL = URL(string: urlString) { 
      if (UIApplication.shared.canOpenURL(phoneCallURL)) { 
       UIApplication.shared.open(phoneCallURL, options: [:], completionHandler: nil) 
      } 
     } 
    } 
0

對於夫特3.1 &向後兼容的方法,這樣做:

@IBAction func phoneNumberButtonTouched(_ sender: Any) { 
    if let number = place?.phoneNumber { 
    makeCall(phoneNumber: number) 
    } 
} 

func makeCall(phoneNumber: String) { 
    let formattedNumber = phoneNumber.components(separatedBy: 
    NSCharacterSet.decimalDigits.inverted).joined(separator: "") 

    let phoneUrl = "tel://\(formattedNumber)" 
    let url:NSURL = NSURL(string: phoneUrl)! 

    if #available(iOS 10, *) { 
     UIApplication.shared.open(url as URL, options: [:], completionHandler: 
     nil) 
    } else { 
    UIApplication.shared.openURL(url as URL) 
    } 
} 
1

的SWIFT 3.0

if let url = URL(string: "tel://\(number)"), UIApplication.shared.canOpenURL(url) { 
    if #available(iOS 10, *) { 
     UIApplication.shared.open(url) 
    } else { 
     UIApplication.shared.openURL(url) 
    } 
} 
else { 
    print("Your device doesn't support this feature.") 
}