Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger another action when tapping a ShareLink

Currently using SwiftUI built in ShareLink functionality. I need to trigger a different action at the same time that ShareLink is pressed. This ShareLink is nested within a SwiftUI Menu.

How would I accomplish this?

Here is an example of my code:

struct ContentView: View {
    var body: some View {
        VStack {
            Menu("options") {
                Button {
                    print("button 1 is pressed")
                } label: {
                    Text("Test Menu Button")
                }
            
                shareLinkButton
                    .onTapGesture {
                        print("ShareLink triggered!")
                    }
            }
        }
    }
    
    
    var shareLinkButton: some View {
        ShareLink(item: URL(string: "https://stackoverflow.com/")!) {
            Label {
                Text("hello")
                    .onTapGesture {
                        print("HELLO")
                    }
            } icon: {
                Image(systemName: "apple.logo")
            }
        }
        .onTapGesture {
            print("ShareLink triggered!")
        }
    }
}

It is just a basic Menu with two buttons in them.

None of the onTapGestures get triggered at any point. I've also tried to wrap ShareLink within a button, like this:

        Button {
            ShareLink(item: URL(string: "https://stackoverflow.com/")!) {
                Label {
                    Text("hello")
                        .onTapGesture {
                            print("HELLO")
                        }
                } icon: {
                    Image(systemName: "apple.logo")
                }
            }
            print("test")
        } label: {
            Label {
                Text("hello")
                    .onTapGesture {
                        print("HELLO")
                    }
            } icon: {
                Image(systemName: "apple.logo")
            }
        }

Which triggers the print statement, but not the ShareLink.

I've also tried to use simultaneousGesture as well, but, no luck.

I do not want to use UIKit for my solution, I'd like to keep this a SwiftUI implementation.

Any suggestions? :)

like image 641
Pete Avatar asked Oct 29 '25 15:10

Pete


1 Answers

I agree with blackhatsrak above. If you and others are still looking for a UIKit solution, here it is:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Menu("options") {
                Button {
                    print("button 1 is pressed")
                } label: {
                    Text("Test Menu Button")
                }

                customShareLinkButton
            }
        }
    }

    var customShareLinkButton: some View {
        Button {
            presentShareLink()
        } label: {
            Label("share", systemImage: "square.and.arrow.up")
        }
    }
    
    func presentShareLink() {
        print("your logic here")
        
        guard let url = URL(string: "https://stackoverflow.com/") else { return }
        let vc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
        let scene = UIApplication.shared.connectedScenes.first { $0.activationState == .foregroundActive } as? UIWindowScene
        scene?.keyWindow?.rootViewController?.present(vc, animated: true)
    }
}

You can replace the print statement in the presentShareLink function with your own logic. Although it looks a bit verbose, it still does the job to present the share link sheet.

like image 68
Yongye Tan Avatar answered Nov 01 '25 10:11

Yongye Tan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!