How to use openURL for making a phone call in Swift?

后端 未结 12 575
囚心锁ツ
囚心锁ツ 2020-11-30 05:30

I have converted the code for making a phone call from Objective-C to Swift, but in Objective-C, we can set the type of the URL that we like to open (e.g. telephone, SMS, we

相关标签:
12条回答
  • 2020-11-30 06:04

    The following code snippet can tell if the SIM is there or not and if the device is capable of making the call and if ok then it'll make the call

      var info = CTTelephonyNetworkInfo()
        var carrier = info.subscriberCellularProvider
        if carrier != nil && carrier.mobileNetworkCode == nil || carrier.mobileNetworkCode.isEqual("") {
           //SIM is not there in the phone
        }
        else if UIApplication.sharedApplication().canopenURL(NSURL(string: "tel://9809088798")!)
        {
        UIApplication.sharedApplication().openURL(NSURL(string: "tel://9809088798")!)
        }
        else    
        {
          //Device does not have call making capability  
        }
    
    0 讨论(0)
  • 2020-11-30 06:04

    Swift 4 and above

    let dialer = URL(string: "tel://5028493750")
        if let dialerURL = dialer {
            UIApplication.shared.open(dialerURL)
    }
    
    0 讨论(0)
  • 2020-11-30 06:05

    For Swift in iOS:

    var url:NSURL? = NSURL(string: "tel://9809088798")
    UIApplication.sharedApplication().openURL(url!)
    
    0 讨论(0)
  • 2020-11-30 06:15

    You need to remember to remove the whitespaces or it won't work:

    if let telephoneURL = NSURL(string: "telprompt://\(phoneNumber.stringByReplacingOccurrencesOfString(" ", withString: ""))") {
            UIApplication.sharedApplication().openURL(telelphoneURL)
        }
    

    "telprompt://" will prompt the user to call or cancel while "tel://" will call directly.

    0 讨论(0)
  • 2020-11-30 06:16

    For making a call in swift, just use the following code: (I have tested it in XCode 11, swift 5)

    let phone = "1234567890"
            if let callUrl = URL(string: "tel://\(phone)"), UIApplication.shared.canOpenURL(callUrl) {
                UIApplication.shared.open(callUrl)
    
    0 讨论(0)
  • 2020-11-30 06:20

    You must insert "+"\ is another way

    private func callNumber(phoneNumber:String) {
      if let phoneCallURL:NSURL = NSURL(string:"tel://"+"\(phoneNumber)") {
        let application:UIApplication = UIApplication.sharedApplication()
        if (application.canOpenURL(phoneCallURL)) {
          application.openURL(phoneCallURL);
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题