0

The code is pretty basic, but using a .onTapGesture() doesn't appear to be the way to go.

The idea is to track the ShareLink interaction for analytics purposes.

ShareLink(item: URL(string: "someUrl")!, message: Text("share_invite_message")) {
    Text("detect me 👋") 
    // ..onTapGesture { let _ = print("tap") } // ShareLink doesn't work
}
// ..onTapGesture { let _ = print("tap") } // tap gesture doesn't work

/* tap gesture doesn't work
.overlay {
    Color.clear.onTapGesture {
        let _ = print("tap")
    }
}
*/

1 Answer 1

2

try this approach using .simultaneousGesture outside the ShareLink

struct ContentView: View {
    var body: some View {
        ShareLink(item: URL(string: "https://stackoverflow.com/")!, message: Text("share_invite_message")){
            Text("detect me 👋") 
        }
        .buttonStyle(.plain)  // <--- for macOS as well
        .simultaneousGesture(TapGesture().onEnded {
            print("----> tap detected")
        })
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

This works on iOS, but does not do anything on macOS.
to make it work on macOS as well, simply add .buttonStyle(.plain) to the ShareLink. Updated my answer.
Correct, that works!
Fantastic! This works 🎉 I seem to remember trying it out but it didn't, maybe I was doing something different. Thank you!
if my answer helped you, consider accepting it by ticking the tick mark next to it, it turns green.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.