In the LargeMetarView which is a Large Size Widget view I added the Link definition to make each airport returned a link and when I click each line entry they act like a link, but just go to the main app start page, not the WidgetStationView I want it to open to show the detail of the selected airport. I have not built the entire view to show all data, I'm just trying to get the link to work and open the WidgetStaionview swiftui view that located in the main app.
struct LargeMetarView: View {
@ObservedObject var metarservice = MetarService()
@State private var skyImage = String()
var entry: MetarEntry
var counter: Int = 0
var body: some View {
ZStack(alignment: .topLeading) {
LinearGradient(gradient: Gradient(colors: [.black.opacity(1), .black.opacity(0.7), .black.opacity(1)]), startPoint: .bottomLeading, endPoint: .topTrailing)
HStack(alignment: .top) {
Text("FRAT Metars")
Text("Updated: ")
Text(entry.date, style: .time)
}
.frame(maxWidth: .infinity)
.padding(10)
.background(.blue)
.foregroundColor(.white)
.clipped()
.shadow(radius: 8)
HStack(alignment: .top, spacing: 8) {
VStack(alignment: .leading, spacing: 5){
Spacer()
ForEach(entry.metars) { entries in
**Link(destination: URL(string: "frat://metar/\(entries.mairport)")!)**{
HStack(spacing: 5){
switch entries.mfltcat {
case "VFR":
Image(systemName: entries.metarImage)
.renderingMode(.original)
.font(.system(size: 25))
.foregroundColor(.green)
.shadow(color: .black, radius: 1, x: 0.5, y: 0.5)
case "MVFR":
Image(systemName: entries.metarImage)
.renderingMode(.original)
.font(.system(size: 25))
.foregroundColor(.cyan)
.shadow(color: .black, radius: 1, x: 0.5, y: 0.5)
case "IFR":
Image(systemName: entries.metarImage)
.renderingMode(.original)
.font(.system(size: 25))
.foregroundColor(.red)
.shadow(color: .black, radius: 1, x: 0.5, y: 0.5)
case "LIFR":
Image(systemName: entries.metarImage)
.renderingMode(.original)
.font(.system(size: 25))
.foregroundColor(.purple)
.shadow(color: .black, radius: 1, x: 0.5, y: 0.5)
default:
Image(systemName: entries.metarImage)
.renderingMode(.original)
.font(.system(size: 25))
.foregroundColor(.green)
.shadow(color: .black, radius: 1, x: 0.5, y: 0.5)
}
Text(entries.mairport)
switch entries.mfltcat {
case "VFR":
Text(entries.mfltcat).bold()
.foregroundColor(Color.green)
case "MVFR":
Text(entries.mfltcat).bold()
.foregroundColor(Color.cyan)
case "IFR":
Text(entries.mfltcat).bold()
.foregroundColor(Color.red)
case "LIFR":
Text(entries.mfltcat).bold()
.foregroundColor(Color.purple)
default:
Text(entries.mfltcat)
.foregroundColor(Color.white).bold()
}
Text(entries.mobstime + "Z")
Text(entries.mvis)
Text(entries.mwind)
} // End of HSTACK
}
.foregroundColor(.white)
.font(.subheadline)
Divider()
.frame(height: 2)
.overlay(.white)
}
}
.padding(20)
}.onAppear(perform: WidgetCenter.shared.reloadAllTimelines) // HSTACK
Spacer()
} // ZSTACK
//.widgetURL(URL(string: "metarnav"))
} // End of someview
struct LargeMetarView_Previews: PreviewProvider {
static var previews: some View {
LargeMetarView(entry: MetarEntry(date: .now, metars: tempData))
.previewContext(WidgetPreviewContext(family: .systemLarge))
}
}
}
import SwiftUI
import WidgetKit
struct WidgetStationView: View {
var mdm: MetarDataModel
var selectedStation: MetarDataModel?
var metarservice = MetarService()
var body: some View {
HStack {
Text("Metar Data")
}
GroupBox {
VStack(alignment: .leading) {
Text("\(mdm.mairport)")
.font(.headline)
}
} label: {
Label("Metar #\(mdm.mairport)", systemImage: "person")
} // Link(destination: URL(string: "frat://metar/\(entries.mairport)")!)
.padding()
.onOpenURL { url in
guard
url.scheme == "frat",
url.host == "metar",
url.path == mdm.mairport else {
return
}
Task {
// do {
let stationWx = await getStation(with: mdm.mairport)
DispatchQueue.main.async {
print(stationWx)
}
// } catch {
// }
}
} // end of open url
} // end of view
}
func getStation(with apt: String) async -> [MetarDataModel] {
let metarservice = MetarService()
var metars = [MetarDataModel]()
do {
metars = try await metarservice.fetchMetars(metarAPTs: apt)
} catch {
}
return metars
}
I have tried several solutions from stack, reddit, medium, etc., but nothing seems to open the swiftui view I want to. I am new to swiftui as most of my app was developed with UIKit and think I'm dropping the ball with defining the url sheme, host and path? Any help would be appreciated.