How Can I Handle Tap Gesture on Widget?

1.7k views Asked by At

I'm writing a widget with WidgetKit and I want to make the widget's content is clickable. For example, if users click to standings, I want to open the standings tab when the app becomes active.

I tried to use notification between the app and widget but the tap gesture is not working, I added print inside of the tap gesture but it did not appear in the console. Also, I added the same app group to both of them.

WidgetView:

struct LargeWidget : View {
    
    @State var standings : [StandingsTable]
    
    var body: some View {
        VStack(alignment:.leading){
            if standings.count > 0{
                HStack(spacing:5){
                    Text("#").foregroundColor(.gray)
                        .frame(width: 30)
                    Text("Team".localized).foregroundColor(.gray)
                    Spacer()
                    Text("_D".localized).foregroundColor(.gray)
                        .frame(width: 30)
                    Text("_L".localized).foregroundColor(.gray)
                        .frame(width: 30)
                    Text("_W".localized).foregroundColor(.gray)
                        .frame(width: 30)
                    Text("_P".localized).foregroundColor(.gray)
                        .frame(width: 30)
                }
                Divider()
                ForEach(0..<5, id: \.self) { i in
                    HStack(spacing:5){
                        Text(standings[i].rank)
                            .font(.system(size: 15))
                            .padding(.vertical, 3)
                            .frame(width: 30)
                            .background(Color(UIColor.systemBackground))
                            .cornerRadius(4)
                        Text(standings[i].name)
                            .lineLimit(1)
                            .padding(.leading, 5)
                        Spacer()
                        Text(standings[i].drawn)
                            .frame(width: 30)
                        Text(standings[i].lost)
                            .frame(width: 30)
                        Text(standings[i].won)
                            .frame(width: 30)
                        Text(standings[i].points)
                            .frame(width: 30)
                    }
                    .padding(.vertical, 5)
                    .background(standings[i].name == "Besiktas" ? Color(UIColor.systemGray6) : Color.clear)
                    .cornerRadius(8)
                    
                }
                Spacer(minLength: 0)
            }else{
                Text("Large")
                    .padding()
            }
        }.padding()
        .onTapGesture {
            print("clicked to standings")
            DispatchQueue.main.async(execute: {
                NotificationCenter.default.post(name: NSNotification.Name("standings"), object: nil, userInfo: nil)
            })
        }
    }
}

and here ContentView in app:

import SwiftUI

extension NSNotification {
    static let openStandings = NSNotification.Name.init("standings")
}

struct ContentView: View {
    
    @State var show: Bool = false
    
    var body: some View {
        NavigationView{
            Text("Hello, world!")
                .padding()
        }.sheet(isPresented: self.$show) {
            VStack{
                Text("Notification")
                    .padding()
            }
        }
        .onReceive(NotificationCenter.default.publisher(for: NSNotification.openStandings))
        { obj in
            self.show.toggle()
        }
    }
}

Screenshot of Widget

1

There are 1 answers

0
Halil İbrahim YÜCE On BEST ANSWER

Okay I created a new project with SwiftUI Environments (not old way AppDelegate and SceneDelegate).

Then I use Link for tap actions and I got it with .onOpenURL modifier in ContentView. It works :)

ContentView:

import SwiftUI

enum SelectedTab: Hashable {
    case home
    case standings
    case options
}

struct ContentView: View {
    
    @State var selectedTab: SelectedTab = .home
    
    var body: some View {
        TabView(selection: self.$selectedTab) {
            Text("Home")
                .tabItem {
                    Image(systemName: "house")
                        .renderingMode(.template)
                    Text("Home")
                }.tag(SelectedTab.home)
            Text("Standings")
                .tabItem {
                    Image(systemName: "list.number")
                        .renderingMode(.template)
                    Text("Standings")
                }.tag(SelectedTab.standings)
            Text("Options")
                .tabItem {
                    Image(systemName: "gear")
                        .renderingMode(.template)
                    Text("Options")
                }.tag(SelectedTab.options)
                .onOpenURL { (url) in
                    if url.absoluteString == "widget-deeplink://standings"{
                        self.selectedTab = .standings
                    }
                }
        }
    }
}

Link usage example in Widget:

Link(destination: URL(string: "widget-deeplink://standings")!) {
    Text("Link Test")
}