iOS Native Setup

Overview

This guide includes client-side set up information for iOS. The EC is implemented into your iOS app using frameworks that are already part of the Swift programming language.

API Sequence

Demo

You can see the results of installing the Enforcement Challenge on iOS in this screen capture recording of the example project.

.

 

Client-side Setup for iOS

Xcode Environment Setup

  1. For this example, you will need to create a directory within your application called Web_Assets. The code in this example is looking for an HTML document called index.html in that directory.

     

  2. The index.html document must contain the client side EC code which enables the EC to render properly within the WebView.
    The following code is an example of what index.html must contain to invoke a basic EC. In this example, replace "YOUR_PUBLIC_KEY_GOES_HERE" with the Public Key provided by Arkose Labs.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <html>     <head>         <script src="https://client-api.arkoselabs.com/fc/api/?onload=loadChallenge" async defer></script>         <script type="text/javascript">             function loadChallenge() {                 new ArkoseEnforcement({                                public_key: "YOUR_PUBLIC_KEY_GOES_HERE",                                target_html: "CAPTCHA",                                callback: function() {                                console.log("played");                                }                                });             }         </script>     </head>     <body>         <form method="post" target="_self">             <p><label for="name">Name</label><input type="text" name="name"></p>                 <div id="CAPTCHA"></div>                 <input name="Submit" type="submit" value="Submit">         </form>         </div>     </body> </html>

Invoking the EC through a ViewController

  1. The following Swift syntax is needed to invoke the EC via the WebView within the app:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 import UIKit import WebKit // Wrap the WKWebView webview to allow IB use class MyWebView : WKWebView {     required init?(coder: NSCoder) {         let configuration = WKWebViewConfiguration()         let controller = WKUserContentController()         configuration.userContentController = controller;         super.init(frame: CGRect.zero, configuration: configuration)     } } class ViewController: UIViewController {     @IBOutlet weak var webView: MyWebView!     override func viewDidLoad() {         super.viewDidLoad()         // Choose to load a file or a string         let loadFile = false         if let filePath = Bundle.main.path(forResource:"index", ofType:"html", inDirectory: "Web_Assets") {             if (loadFile) {                 // load file                 let filePathURL = URL.init(fileURLWithPath: filePath)                 let fileDirectoryURL = filePathURL.deletingLastPathComponent()                 webView.loadFileURL(filePathURL, allowingReadAccessTo: fileDirectoryURL)             } else {                 do {                     // load html string - baseURL needs to be set for local files to load correctly                     let html = try String(contentsOfFile: filePath, encoding: .utf8)                     webView.loadHTMLString(html, baseURL: Bundle.main.resourceURL?.appendingPathComponent("Web_Assets"))                 } catch {                     print("Error loading html")                 }             }         }     }

Once the challenge is invoked, you need to collect the Session Token and pass it to your server with the flow data for validation. An example of how to do this in Swift would look like the code below:

1 let sessionToken = WebView.stringByEvaluatingJavaScriptFromString("document.getElementById('FunCaptcha-Token').value")

Server-Side Instructions

When you have successfully set up the client-side you must go on to the server-side instructions.