Giter Club home page Giter Club logo

recaptcha_objc's Introduction

ReCaptcha_objc

Version License Platform


This project is an Objc version of ReCaptcha.


Add Google's Invisible ReCaptcha v2 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.

Example Gif 2 Example Gif

Warning ⚠️

Beware that this library only works for ReCaptcha v2 Invisible keys! Make sure to check the reCAPTCHA v2 Invisible badge option when creating your API Key.

ReCaptcha v2 invisible key example

You won't be able to use a ReCaptcha v3 key because it requires server-side validation. On v3, all challenges succeed into a token which is then validated in the server for a score. For this reason, a frontend app can't know on its own wether or not a user is valid since the challenge will always result in a valid token.

Installation

ReCaptcha_objc is available through CocoaPods To install it, simply add the following line to your dependencies file:

Cocoapods

pod "ReCaptcha_objc"

Usage

The reCAPTCHA keys can be specified as Info.plist keys or can be passed as parameters when instantiating ReCaptcha().

For the Info.plist configuration, add ReCaptchaKey and ReCaptchaDomain (with a protocol ex. http:// or https://) to your Info.plist and run:

ReCaptcha *recaptcha = [[ReCaptcha alloc] initWithApiKey:nil baseURL:nil endpoint:EndpointDefault locale:nil error:nil];

[recaptcha configureWebView:^(WKWebView * _Nonnull webview) {
    self->webview = webview;
    self->webview.frame = self.view.bounds;
    self->webview.tag = webViewTag;

    // For testing purposes
    // If the webview requires presentation, this should work as a way of detecting the webview in UI tests
    self->label = [[UILabel alloc] initWithFrame:self.view.bounds];
    self->label.numberOfLines = 0;
    self->label.tag = testLabelTag;
    [self.view addSubview:self->label];
}];

dispatch_async(dispatch_get_main_queue(), ^{
    [self->recaptcha validateOn:nil resetOnError:YES completion:^(NSString * _Nonnull result) {
        [self->webview removeFromSuperview];
        self->label.text = result;
        NSLog(@"Result:%@", result);
    }];
});

If instead you prefer to keep the information out of the Info.plist, you can use:

ReCaptcha *recaptcha = [[ReCaptcha alloc] initWithApiKey:"YOUR_RECAPTCHA_KEY" baseURL:"YOUR_RECAPTCHA_DOMAIN" endpoint:EndpointDefault locale:nil error:nil];
...

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:

typedef SWIFT_ENUM(NSInteger, Endpoint, closed) {
/// Google’s default endpoint. Points to
/// https://www.google.com/recaptcha/api.js
  EndpointDefault = 0,
/// Alternate endpoint. Points to https://www.recaptcha.net/recaptcha/api.js
  EndpointAlternate = 1,
};

ReCaptcha *recaptcha = [[ReCaptcha alloc] initWithApiKey:"YOUR_RECAPTCHA_KEY" baseURL:"YOUR_RECAPTCHA_DOMAIN" endpoint:EndpointAlternate locale:nil error:nil];

Help Wanted

Do you love ReCaptcha and work actively on apps that use it? We'd love if you could help us keep improving it! Feel free to message us or to start contributing right away!

License

ReCaptcha/ReCaptcha_objc is available under the MIT license. See the LICENSE file for more info.

recaptcha_objc's People

Stargazers

咸菜 avatar Adelar da Silva Queiróz avatar Phil Chang avatar

Watchers

Adelar da Silva Queiróz avatar James Cloos avatar Phil Chang avatar  avatar

Forkers

willyue

recaptcha_objc's Issues

Fatal Error

When the resetOnError is set to NO and after the first validation completed it will get Fatal Error if we keep the app open for some period of time. It happens to the demo project as well.

public func validate(on view: UIView, resetOnError: Bool = true, completion: @escaping (String) -> Void) {
let completion2 = { [weak self] (result: ReCaptchaResult) in
print(try? result.dematerialize())
completion(try! result.dematerialize()) < ---- fatal errror on this line
}

  manager.shouldResetOnError = resetOnError
  manager.completion = completion2

  manager.validate(on: view)

}

ReCaptcha_objc/ReCaptcha.swift:195: Fatal error: 'try!' expression unexpectedly raised an error: ⚠️ WARNING! ReCaptcha wasn't successfully configured. Please double check your ReCaptchaKey and ReCaptchaDomain.
Also check that you're using ReCaptcha's SITE KEY for client side integration.
2022-01-05 00:43:45.644481+0800 demo[6399:195672] ReCaptcha_objc/ReCaptcha.swift:195: Fatal error: 'try!' expression unexpectedly raised an error: ⚠️ WARNING! ReCaptcha wasn't successfully configured. Please double check your ReCaptchaKey and ReCaptchaDomain.
Also check that you're using ReCaptcha's SITE KEY for client side integration.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.