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!