Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create tappable url/phone number in SwiftUI

Tags:

I would like to display a phone number in a SwiftUI Text (or any View), and then make it clickable so that it will open the 'Phone'.

Is there a way to do this with SwiftUI, or should I try to wrap a UITextView in SwiftUI and do it the old-fashioned way with NSAttributed string etc?

I've read the documentation for Text in SwiftUI, and couldn't find anything about how to do this. Currently trying to do this in Xcode 11 beta 5.

I've searched 'text' in the SwiftUI API in SwiftUI.h

I've also searched stackoverflow [swiftui] and google with queries like "make phone number/url tappable", "Tappable link/url swiftUI" etc..

Text("123-456-7890")     .onTapGesture {     // do something here }  

(text will be Japanese phone number)

like image 889
nSquid Avatar asked Aug 20 '19 23:08

nSquid


People also ask

How do I create a link in SwiftUI?

To create a link, enclose the link text in brackets (e.g., [Duck Duck Go]) and then follow it immediately with the URL in parentheses (e.g., (https://duckduckgo.com)).


2 Answers

Using iOS 14 / Xcode 12.0 beta 5

Use new link feature in SwiftUI for phone and email links.

    // Link that will open Safari on iOS devices     Link("Apple", destination: URL(string: "https://www.apple.com")!)          //  Clickable telphone number     Link("(800)555-1212", destination: URL(string: "tel:8005551212")!)      //  Clickable Email Address     Link("[email protected]", destination: URL(string: "mailto:[email protected]")!)      
like image 54
Steve Dugan Avatar answered Oct 18 '22 09:10

Steve Dugan


Try this,

let strNumber = "123-456-7890"  Button(action: {     let tel = "tel://"     let formattedString = tel + strNumber      guard let url = URL(string: formattedString) else { return }     UIApplication.shared.open(url)     }) {    Text("123-456-7890") } 
like image 22
Ashish Avatar answered Oct 18 '22 10:10

Ashish