Krish Mehra Krish Mehra - 2 months ago 26
iOS Question

How do I open this URL to make a phone call in swift?

When I tried to open let url in swift using UIApplication.shared.openURL(url!), it would call the right number but it would add weird digits before it. For example if I type 2345678901 as a number it'll show weird digits (http://imgur.com/a/WInVb) What should I do to open the let url? Here is the code:

import UIKit

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBOutlet weak var myText: UITextField!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func btn(_ sender: AnyObject) {
var numbers = self.myText.text

let comp = NSURLComponents()
comp.scheme = "tel"
comp.host = "\(numbers)"
let url = comp.url


}
}

Answer

You are using swift 3.0 so use open because openUrl is deprecated.

if let url = URL(string: "tel://\(number)") {
    UIApplication.shared.open(url, options: [:])
}
Comments