Integrating reCAPTCHA Enterprise with iOS apps

Stay organized with collections Save and categorize content based on your preferences.

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. iOS SDK is accessible after a security review. Contact our sales team to onboard your site to this feature.

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

  4. Depending on your integration method, ask your sales representative to grant you access to one of the following: reCAPTCHA Enterprise CocoaPods repository, the Swift Package Manager repository, or the iOS SDK download.

  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.

      The reCAPTCHA Enterprise pod is fairly large in size. However, after reCAPTCHA Enterprise is installed, due to dynamic linking the size of the user's IPA and the users installed app are approximately 598 KB and 1.786 MB respectively.

    • Download the SDK using CocoaPods, Swift Package Manager 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", "17.0.0"
        
      3. Install the required dependencies by running pod update.

      Swift Package Manager

      1. In XCode, select File > Add Packages, and enter the following URL in the Search or Enter Package URL field: https://github.com/GoogleCloudPlatform/recaptcha-enterprise-mobile-sdk
      2. In the XCode dialog box, enter the GitHub username you gave to your sales representative for the account and a personal access token created using GitHub's instructions. The Personal Access Token must have the scopes listed in the XCode Sign In dialog.

        Xcode installs the SDK and its required dependencies.

      Direct download

      Download the SDK binary 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