Integrate Google Cloud Fraud Defense with iOS apps | Google Cloud Documentation
Skip to main content Console English Deutsch Español Español – América Latina Français Indonesia Italiano Português Português – Brasil עברית 中文 – 简体 中文 – 繁體 日本語 한국어 Sign in Google Cloud Fraud Defense Start free reCAPTCHA is a part of Google Cloud Fraud Defense. For more information, see Google Cloud Fraud Defense . Home Documentation Security Google Cloud Fraud Defense Guides Integrate Google Cloud Fraud Defense with iOS apps Stay organized with collections Save and categorize content based on your preferences.
You can integrate Google Cloud Fraud Defense into iOS apps to evaluate client device signals and protect mobile user interactions against automated abuse. Due to the variation in mobile devices in terms of screen size, performance, and UIs of the apps, the visual checkbox reCAPTCHA challenge ( I'm not a robot ) is not available for iOS mobile apps. You can instead implement your own tiered enforcement strategy, such as an MFA flow to provide an alternative redemption path for suspicious traffic.
The SDK makes use of reflection and dynamic code to allow for the update and refinement of the detection system in already deployed applications or SDKs. To avoid interference with the application, the classes available in the system are restricted to a carefully controlled list. Before you begin Set the minimum SDK of your app to iOS 15 or create a new mobile app. Prepare your environment for Google Cloud Fraud Defense .
Create a reCAPTCHA key for the iOS app platform. Alternatively, you can copy the ID of an existing reCAPTCHA key for iOS by performing one of the following steps: To copy the ID of an existing key from the Google Cloud console , do the following: Go to the Google Cloud Fraud Defense page. Go to Google Cloud Fraud Defense Select the Keys tab. In the reCAPTCHA keys list, find the key you want to copy, and then click content_copy .
To copy the ID of an existing key using the REST API , use the projects.keys.list method. To copy the ID of an existing key using the gcloud CLI , use the gcloud recaptcha keys list command. Have a GitHub account. Read the Apple privacy details . Prepare your iOS environment 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 the SDK by using one of the following: CocoaPods Download and install CocoaPods . Create a Podfile and add the following lines to your Podfile: source "https://github.com/CocoaPods/Specs.git" target 'AppTarget' do # Podfiles must include use_frameworks! or # use_frameworks! :linkage => :static use_frameworks! pod "RecaptchaEnterprise" , "18.10.0-beta01" ... end Install the required dependencies by running pod update .
Swift Package Manager 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 In the Xcode dialog, enter the following details: GitHub username. A personal access token that you 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. Flutter For detailed instructions about using reCAPTCHA through Flutter, see the Flutter documentation . React Native For detailed instructions about using reCAPTCHA through React Native, see the React Native documentation . Direct download If you want to download the SDK and its dependencies as xcframeworks, download the client . Integrate Fraud Defense with your iOS app To integrate Fraud Defense with your iOS app, follow these steps in Xcode: To instantiate the SDK with the reCAPTCHA key (KEY_ID) that you created, update the app with the following code: Swift with Storyboard Update ViewController.swift . import RecaptchaEnterprise class ViewController : UIViewController { var recaptchaClient : RecaptchaClient ? override func viewDidLoad () { super . viewDidLoad () Task { do { self . recaptchaClient = try await Recaptcha . fetchClient ( withSiteKey : " KEY_ID " ) } catch let error as RecaptchaError { print ( "RecaptchaClient creation error: \( String ( describing : error . errorMessage )) ." ) } } } } If your application's minimum OS version is less than 13, use a trailing closure instead: import RecaptchaEnterprise class ViewController : UIViewController { var recaptchaClient : RecaptchaClient ? override func viewDidLoad () { super . viewDidLoad () Recaptcha . fetchClient ( withSiteKey : " KEY_ID " ) { client , error in guard let client = client else { print ( "RecaptchaClient creation error: \( error ) ." ) return } self . recaptchaClient = client } } } Swift with SwiftUI Create a ViewModel class. import RecaptchaEnterprise @ MainActor class ViewModel : ObservableObject { private var recaptchaClient : RecaptchaClient ? init () { Task { do { self . recaptchaClient = try await Recaptcha . fetchClient ( withSiteKey : " KEY_ID " ) } catch let error as RecaptchaError { print ( "RecaptchaClient creation error: \( String ( describing : error . errorMessage )) ." ) } } } } If your application's minimum OS version is less than 13, use a trailing closure instead: import RecaptchaEnterprise class ViewController : UIViewController { var recaptchaClient : RecaptchaClient ? override func viewDidLoad () { super . viewDidLoad () Recaptcha . fetchClient ( withSiteKey : " KEY_ID " ) { client , error in guard let client = client else { print ( "RecaptchaClient creation error: \( error ) ." ) return } self . recaptchaClient = client } } } Instantiate ViewModel in ContentView.swift . import SwiftUI import RecaptchaEnterprise struct ContentView : View { @ StateObject private var viewModel = ViewModel () var body : some View { } } struct ContentView_Previews : PreviewProvider { static var previews : some View { ContentView () } } Objective-C If your app is written in Objective-C, create a dummy Swift file and include the following import to make sure Xcode can find and link the Swift libraries. import Foundation To ensure that the Swift code is linked correctly, navigate to Target > Build Settings > Always Embed Swift Standard Libraries and verify that the option is set to Yes .
Update ViewController.h . #import <RecaptchaEnterprise/RecaptchaEnterprise.h> @interface ViewController : UIViewController @property ( strong , atomic ) RecaptchaClient * recaptchaClient ; @end Update ViewController.m . @implementation ViewController [ Recaptcha fetchClientWithSiteKey : @" KEY_ID " completion : ^ void ( RecaptchaClient * recaptchaClient , NSError * error ) { if ( ! recaptchaClient ) { NSLog ( @"%@" , ( RecaptchaError * ) error . errorMessage ); return ; } self - > _recaptchaClient = recaptchaClient ; } ]; @end Initialization of the SDK can take several seconds to complete.
To mitigate this latency, initialize the client as early as possible, such as during the onCreate() call of a custom Application class. You shouldn't make UI elements block on the reCAPTCHA SDK. Create a button to call reCAPTCHA and trigger execute() . Swift with Storyboard In the storyboard, create a button. Create an action in ViewController linked to the button that you created. Call the execute() method passing a Login action to return a reCAPTCHA token by using the following code snippet: guard let recaptchaClient = recaptchaClient else { print ( "RecaptchaClient creation failed." ) return } Task { do { let token = try await recaptchaClient . execute ( withAction : RecaptchaAction . login ) print ( token ) } catch let error as RecaptchaError { print ( error . errorMessage ) } } If your application's minimum OS version is less than 13, use a trailing closure instead: guard let recaptchaClient = recaptchaClient else { print ( "RecaptchaClient creation failed." ) return } recaptchaClient . execute ( withAction : RecaptchaAction . login ) { token , error in if let token = token { print ( token ) } els
The rest of this page is on the live site.