Once installation is complete and pre-requisites are met, we are ready to proceed with the actual implementation:
var hcaptcha: HCaptcha!
// ...
hcaptcha.rx.validate(on: view)
.subscribe(onNext: { (token: String) in
// Do something
})Note: caller code is responsible for hiding the WebView after challenge processing. This is illustrated in the Example app, and can be achieved with:
...
hcaptcha?.configureWebView { [weak self] webview in
webview.tag = "hCaptchaViewTag"
}
...
let disposeBag = DisposeBag()
let validate = hcaptcha.rx.validate(on: view)
...
validate
.map { [weak self] _ in
self?.view.viewWithTag("hCaptchaViewTag")
}
.subscribe(onNext: { webview in
webview?.removeFromSuperview()
})
.disposed(by: disposeBag)The full code is in ViewController.swift
This SDK allows you to receive interaction events. This is how it can be implemented:
For RxSwift:
let hcaptcha = try? HCaptcha(...)
...
hcaptcha.rx.events()
.subscribe { [weak self] rxevent in
let event = rxevent.element?.0
if event == .open {
...
}
}
...