How to pass a gesture event performed on a SwiftUI view to its internal UIViewControllerRepresentable?

I have a Sign In With Apple button in UIViewControllerRepresentable nested inside a SwiftUI view. Despite the wrapped SwiftUI view being able to get the tap event, the "Sign in With Apple" button nested inside could not get the tap event recognized.

Is there a way to pass the tap event on SwiftUI to UIButton inside?

/// Inside SwiftUI
DCAppleIDButtonView(
    onStart: settingsViewModel.handleLoginStart,
    onSuccess: settingsViewModel.updateCurrentUserLoginState,
    onError: settingsViewModel.handleLoginEnd
)
.onTapGesture {
    print(">>> tap")
}

onTapGesture could be triggered when DCAppleIDButtonView view get tapped.

/// Inside UIViewControllerRepresentable
private func setupAuthorizationAppleIDProviderLoginView() {
    let button = ASAuthorizationAppleIDButton(authorizationButtonType: .continue, authorizationButtonStyle: .white)

    button.addTarget(coordinator, action: #selector(handleAuthorizationAppleIDButtonPress), for: .touchUpInside)

    view.addSubview(button)

    button.translatesAutoresizingMaskIntoConstraints = false
    button.snp.makeConstraints { make in
        make.top.equalTo(view.snp.top)
        make.bottom.equalTo(view.snp.bottom)
        make.leading.equalTo(view.snp.leading)
        make.trailing.equalTo(view.snp.trailing)
    }
}

However, a tap on the `button’ does not seem to be triggered by a tap action.

Leave a Reply

Your email address will not be published.