0

I'm trying to use the Link control in my SwiftUI View, that takes data from a JSON file.

Here's the code:

import SwiftUI
import URLImage

struct ContactsDetail: View {
    
    var busso: BussoModel
    
    var body: some View {
        ScrollView {
            URLImage(URL(string: busso.fotoUrl) ?? furl)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .clipped()
                .padding(.bottom)
            Text(busso.testo)
                .font(.body)
                .padding(.bottom)
            Link(busso.extra1, destination: URL(string: busso.extra1)!)
                .font(.headline)
                .foregroundColor(Color(red: 133.0/255.0, green: 114.0/255.0, blue: 159.0/255.0))
                .padding(.bottom)
            Link(busso.extra2, destination: URL(string: busso.extra2)!)
                .font(.headline)
                .foregroundColor(Color(red: 133.0/255.0, green: 114.0/255.0, blue: 159.0/255.0))
        }
        .padding(.top, 8)
        .navigationTitle(busso.titolo)
        .navigationBarTitleDisplayMode(.large)
    }
}

struct ContactsDetail_Previews: PreviewProvider {
    static var previews: some View {
        ContactsDetail(busso: BussoModel.init(id: String(), titolo: String(), autore: String(), testo: String(), data: String(), extra1: String(), extra2: String(), foto: String(), fotoUrl: String()))
    }
}

If I set Link the way you see in the code, the selected JSON fields ("extra1" and "extra2", respectively phone number and mail) appears in the View and I can tap on them, but they don't work...so I changed the code in this way:

Link(busso.extra1, destination: URL(string: "tel:busso.extra1")!)
Link(busso.extra2, destination: URL(string: "mailto:busso.extra2")!)

Now I can tap on them...and they work, but not correctly: in fact, tapping on "extra1" I can call "busso.extra1" and not the phone number (a string) contained in that JSON field...same thing for "extra2", I can send a mail to "busso.extra2" and not the mail address (also a string) contained in that JSON field...

It is obvious that something in the code is wrong, what changes should I make? Thanks!

  • In swift you can use \ ( ) to include a variable as text within a String. So change to "tel:\ (busso.extra1)". Aside from that, Link() is used to open a URL , like "https//www.stackoverflow.com" would open in a browser. What you have don't look like valid URLs. – nicksarno Jan 17 '21 at 05:13
  • @nicksarno Oh, I completely forgot that! :) Anyway, you're right ... according to Apple's official documentation, Link can only be used for URLs...but I found this question (https://stackoverflow.com/questions/57582653/how-to-create-tappable-url-phone-number-in-swiftui), and in Steve Dugan's answer is mentioned the possibility of using it also for telephone numbers and email addresses, so that's why I tried that way, but as I said I completely forgot the backslash trick :) Thank you for your suggestion! – pierofrezza Jan 17 '21 at 11:12

0 Answers0