简体   繁体   中英

How to create pop up when user clicks phone number - iOS10 Swift 3

I have a IBAction where i am trying to make it so that when a user clicks this button, it prompts them with a pop up to either Cancel or Call the number. What i have below does not work.

@IBAction func PhoneBtn(_ sender: Any) {
    if let url = NSURL(string: "1236541234") {
        UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
    }
}

You'll want to use the tel protocol like this:

"tel://123651234"

Including this instructs the OS how to handle the URL. Implementing that, your code would then be:

@IBAction func PhoneBtn(_ sender: Any) {
    if let url = NSURL(string: "tel://1236541234") {
        UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
    }
}

I would also suggest just using the URL API rather than NSURL with an eventual cast to URL . There is a string initializer for URL as well:

URL(string: "tel://1236541234")

Let me know if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM