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

后端 未结 12 574
囚心锁ツ
囚心锁ツ 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 05:54

    @ confile:

    The problem is that your solution does not return to the app after the phone call has been finished on iOS7. – Jun 19 at 13:50

    &@ Zorayr

    Hm, curious if there is a solution that does do that.. might be a restriction on iOS.

    use

    UIApplication.sharedApplication().openURL(NSURL(string: "telprompt://9809088798")!)
    

    You will get a prompt to Call/Cancel but it returns to your application. AFAIK there is no way to return (without prompting)

    0 讨论(0)
  • 2020-11-30 05:56

    I am pretty sure you want:

    UIApplication.sharedApplication().openURL(NSURL(string: "tel://9809088798")!)
    

    (note that in your question text, you put tel//:, not tel://).

    NSURL's string init expects a well-formed URL. It will not turn a bunch of numbers into a telephone number. You sometimes see phone-number detection in UIWebView, but that's being done at a higher level than NSURL.

    EDIT: Added ! per comment below

    0 讨论(0)
  • 2020-11-30 05:57

    For swift 4:

    func call(phoneNumber: String) {
        if let url = URL(string: phoneNumber) {
            if #available(iOS 10, *) {
                UIApplication.shared.open(url, options: [:],
                                          completionHandler: {
                                            (success) in
                                            print("Open \(phoneNumber): \(success)")
                })
            } else {
                let success = UIApplication.shared.openURL(url)
                print("Open \(phoneNumber): \(success)")
            }
        }
    } 
    

    Then, use the function:

    let phoneNumber = "tel://+132342424"
    call(phoneNumber: phoneNumber)
    
    0 讨论(0)
  • 2020-11-30 05:58

    Small update to Swift 3

    UIApplication.shared.openURL(NSURL(string: "telprompt://9809088798")! as URL)
    
    0 讨论(0)
  • 2020-11-30 05:59

    For swift 3

    if let phoneCallURL:URL = URL(string:"tel://\(phoneNumber ?? "")") {
                let application:UIApplication = UIApplication.shared
                if (application.canOpenURL(phoneCallURL)) {
                    application.open(phoneCallURL, options: [:], completionHandler: nil);
                }
            }
    
    0 讨论(0)
  • 2020-11-30 06:04

    A self-contained solution in Swift:

    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);
        }
      }
    }
    

    Now, you should be able to use callNumber("7178881234") to make a call.

    0 讨论(0)
提交回复
热议问题