1

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.

ZDigitalPro
  • 17
  • 1
  • 6
  • Does this help? https://stackoverflow.com/a/75112555/123632 – Ashley Mills Feb 17 '23 at 22:31
  • For my other two widgets, small and medium I defined a scheme in the target as mention in the link you sent and added the code in my AppDelegate to open a UIViewController in my main app which works great. I wanted to make a swiftui view for the Widget to transition to with different information and did not want to use the main apps UIViewController. – ZDigitalPro Feb 17 '23 at 22:47
  • Does this answer your question? [Listen to link click in SwiftUI TextView](https://stackoverflow.com/questions/75192976/listen-to-link-click-in-swiftui-textview) – lorem ipsum Feb 18 '23 at 01:06
  • Thanks Lorem, it gets me closer and I have a direction to go, but I'm not there yet. I have found a lot of articles to go from SwiftUI to UIKit view controller and vise versa, but not much other than what I have tried so far. I'm going from a Widget Extension view to a SwiftUI view that was placed in the main app which was originally built with UIKit. Since a Widget can't use a NavigationView or a NavigationLink, I have to use a Link() instead and the way mine is written the SwiftUI view doesn't receive the URL? – ZDigitalPro Feb 18 '23 at 11:49

0 Answers0