Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HELP] listen to events in swiftUI #171

Closed
EduarRugamas opened this issue Dec 11, 2024 · 4 comments
Closed

[HELP] listen to events in swiftUI #171

EduarRugamas opened this issue Dec 11, 2024 · 4 comments
Assignees

Comments

@EduarRugamas
Copy link

Hello development team

I have a question regarding listening to Hcaptcha events in SwiftUI

since your example does not show how to listen

according to your documentation in the UIKIT example

It is defined in the method where the webview is configured

but it gives me the following problem

When you start the app and load the Hcaptcha for the first time, it apparently shows the event heard
but then when you take on the challenge and be successful
After it expires and I do the challenge again, the events heard are no longer displayed on the console.

Is there any solution to this problem

Thanks for your help in advance

I attach implementation in SwiftUI according to the documentation provided

Captura de pantalla 2024-12-11 a la(s) 13 31 06

Captura de pantalla 2024-12-11 a la(s) 13 31 18

Captura de pantalla 2024-12-11 a la(s) 13 31 28

@CAMOBAP CAMOBAP self-assigned this Dec 11, 2024
@CAMOBAP
Copy link
Collaborator

CAMOBAP commented Dec 11, 2024

@EduarRugamas to be on the same page, you can prevent hcaptcha request again on expiration by passing resetOnError to validate

You mentioned onEvent callback but I don't see it' in the code

Maybe you can explain your scenario, do you need to renew the token on expiration?

@EduarRugamas
Copy link
Author

Hello, I attach a method where to declare the Hcaptcha onEvent

Captura de pantalla 2024-12-12 a la(s) 08 07 54

The behavior I'm trying to get is the following:

When I check for the first time if the event is triggered in the onEvent and if the corresponding print() is displayed in the console
but when the token expires and requests to verify again, the event is no longer triggered nor any other event such as the validate where the token is obtained

@CAMOBAP
Copy link
Collaborator

CAMOBAP commented Dec 15, 2024

It isn't clear when you call configurationWebViewHcaptcha below is a complete example which showsthe event after token expired too

import SwiftUI
import HCaptcha

// Wrapper-view to provide UIView instance
struct UIViewWrapperView: UIViewRepresentable {
    var uiView = UIView()

    func makeUIView(context: Context) -> UIView {
        uiView.backgroundColor = .gray
        return uiView
    }

    func updateUIView(_ view: UIView, context: Context) {
        // nothing to update
    }
}

class HCaptchaViewModel: ObservableObject {
    let hcaptcha: HCaptcha!

    init() {
        self.hcaptcha = try! HCaptcha()
    }

    func configure(_ hostView: UIViewWrapperView) {
        hcaptcha.configureWebView { webview in
            webview.frame = hostView.uiView.bounds
        }
        hcaptcha.onEvent { (event, data) in
            print("HCaptcha onEvent \(event.rawValue)")
        }
    }

    func validate(_ hostView: UIViewWrapperView) {
        hcaptcha.validate(on: hostView.uiView) { result in
            print("HCaptcha result \(try? result.dematerialize())")
        }
    }
}

struct HCaptchaView: View {
    @StateObject var model = HCaptchaViewModel()
    let placeholder = UIViewWrapperView()

    var body: some View {
        VStack{
            placeholder.frame(width: 640, height: 640, alignment: .center)
            Button(
                "validate",
                action: { model.validate(placeholder) }
            ).padding()
        }
        .onAppear {
            model.configure(placeholder)
        }
    }
}

@EduarRugamas
Copy link
Author

Thank you so much

@CAMOBAP CAMOBAP closed this as completed Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants