3.0 KiB
ReCaptcha
Add Google's Invisible ReCaptcha to your project. This library automatically handles ReCaptcha's events and retrieves the validation token or notifies you to present the challenge if invisibility is not possible.
Warning ⚠️
Beware that this library only works for Invisible ReCaptcha keys! Make sure to check the Invisible reCAPTCHA option when creating your API Key.
Installation
ReCaptcha is available through CocoaPods and Carthage. To install it, simply add the following line to your depedencies file:
Cocoapods
pod "ReCaptcha"
# or
pod "ReCaptcha/RxSwift"
Carthage
github "fjcaetano/ReCaptcha"
Carthage will create two different frameworks named ReCaptcha and ReCaptcha_RxSwift, the latter containing the RxSwift
extension for the ReCaptcha framework.
Usage
Simply add ReCaptchaKey and ReCaptchaDomain to your Info.plist and run:
let recaptcha = try? ReCaptcha()
override func viewDidLoad() {
super.viewDidLoad()
recaptcha?.configureWebView { [weak self] webview in
webview.frame = self?.view.bounds ?? CGRect.zero
}
}
func validate() {
recaptcha?.validate(on: view) { [weak self] result in
print(try? result.dematerialize())
}
}
You can also install the reactive subpod and use it with RxSwift:
recaptcha.rx.validate(on: view)
.map { try $0.dematerialize() }
.subscribe(onNext: { (token: String) in
// Do something
})
Alternte endpoint
If your app has firewall limitations that may be blocking Google's API, the JS endpoint may be changed on initialization.
It'll then point to https://www.recaptcha.net/recaptcha/api.js:
public enum Endpoint {
case default, alternate
}
let recaptcha = try? ReCaptcha(endpoint: .alternate) // Defaults to `default` when unset
License
ReCaptcha is available under the MIT license. See the LICENSE file for more info.

