Integrating reCAPTCHA Enterprise with iOS apps

This page explains how to integrate reCAPTCHA Enterprise in your iOS app. Due to the variation in mobile devices in terms of screen size, performance, and UIs of the apps, the visual reCAPTCHA challenge (I'm not a robot) is not available for mobile apps. For an alternative friction method, see Configuring Multi-factor authentication (MFA).

Before you begin

  1. Choose the best method for setting up reCAPTCHA Enterprise in your environment and complete the setup.

  2. Create a reCAPTCHA Key for the iOS app platform.

  3. iOS SDK is accessible after a security review. Contact our sales team to onboard your site to this feature.

  4. Ask your sales representative to grant you access to the reCAPTCHA Enterprise CocoaPods repository and the iOS SDK download page.

  5. To prepare your development environment, do the following:

    • Download and install the latest version of Xcode and create a new blank iOS single view application.
    • Download and install CocoaPods.

    • Download the SDK using CocoaPods or from the download page.

      CocoaPods

      1. Generate a git cookie.
      2. Create a Podfile and add the following lines to your Podfile:

        source "https://cpdc-eap.googlesource.com/RecaptchaEnterprise.git"
        source "https://github.com/CocoaPods/Specs.git"
        
        pod "RecaptchaEnterprise", "16.1.0"
        
      3. Install the required dependencies by running pod update.

      Direct download

      Download the SDK binary directly from the iOS SDK download page.

Configuring the app

Using CocoaPods, you can write your apps in Swift or Objective-C. To configure your app, do the following:

  1. To ensure that -ObjC is listed on your linker flags, navigate to Target > Build Settings > All > Linking and verify that Other Linker Flags shows -ObjC.

  2. If your app is written in Swift, create an Objective-C bridging header and include the following import:

    #import <recaptcha/recaptcha.h>
    

Integrating reCAPTCHA Enterprise with your iOS app

To integrate reCAPTCHA Enterprise with your iOS app, follow these steps in Xcode:

  1. To instantiate the SDK with the reCAPTCHA key that you created, update ViewController with the following code:

    Swift

    let recaptchaClient = RecaptchaClient(siteKey: "reCAPTCHA_KEY_iOS")
    

    Objective-C

    RecaptchaClient *recaptchaClient = [[RecaptchaClient alloc] initWithSiteKey:@"reCAPTCHA_KEY_iOS"];
    

  2. Create a button to call reCAPTCHA Enterprise and trigger the code that you embedded in ViewController:

    1. In the storyboard, create a button.
    2. Create an action in ViewController linked to the button that you created.
    3. Call the execute() method to return a reCAPTCHA token by using the following code snippet:

      Swift

      recaptchaClient.execute(RecaptchaAction(action: .login)) { token, error in
       if let recaptchaToken = token {
              print(recaptchaToken.recaptchaToken)
           }
            if let error = error {
              print(error)
            }
       }
      

      Objective-C

       [recaptchaClient execute: [[RecaptchaAction alloc] initWithAction: RecaptchaActionTypeLogin] onFinishedExecute: ^ (RecaptchaToken * _Nullable token, RCARecaptchaError * _Nullable error) {
      
           if (error) {
               NSLog (@ "% @", error);
           } else {
               NSLog (@ "% @", token);
           }
      
       }];
      
      

  3. Test your application:

    1. To clean your Xcode build environment, from the Product menu, click Clean Build Folder.
    2. To run the application, from the Product menu, click Run.
    3. In your loaded application, click the reCAPTCHA button that you created earlier.
    4. Observe your debug output window for a reCAPTCHA token (alpha-numeric string), which is returned if the integration is successful.

iOS API reference

Contact your sales representative to grant you access to the iOS SDK download page. The iOS SDK download page explains the details of the reCAPTCHA privacy label.

What's next