iOS

How to install, configure and use the Castle iOS SDK

👩‍💻 Looking for the source code? The Castle iOS SDK is available on GitHub.

Installation

Step 1. Configure with API key

Grab your Publishable API Key from the Castle dashboard settings page and configure the SDK:

import Castle

// Place the below in your UIApplicationDelegate application:didFinishLaunchingWithOptions:
Castle.configure(withPublishableKey: "{Publishable-API-Key}")
#import <Castle/Castle.h>

// Place the below in your UIApplicationDelegate application:didFinishLaunchingWithOptions:
[Castle configureWithPublishableKey: "{Publishable-API-Key}"];

The Castle SDK collects fingerprint information from the device and sends this information directly to Castle. These requests are batched to optimize device performance. See the last section, Configuration Notes, to make changes to the way Castle collects and batch-processes this fingerprint information.

Step 2. Forward the fingerprint

The Castle iOS SDK generates a value that we call the request_token. Forward the Castle request_token in relevant requests to your application server.

The request_token value contains a proprietary signature of the mobile device fingerprint. This value should not be used as an identifier. It should be treated as a token. The value will change over time.

You will need to forward the request token as a request header to every request to your API. You can specify this header name in the SDK configuration. The default value is X-Castle-Request-Token.

// NSURLRequest
request.setValue(
  Castle.createRequestToken(),
  forHTTPHeaderField: CastleRequestTokenHeaderName
)
// NSURLRequest
[request setValue:[Castle createRequestToken] forHTTPHeaderField:CastleRequestTokenHeaderName];

Step 3. (optional) Additional configuration

  • The SDK allows for additional configuration options. Please refer to the documentation packaged with the SDK code for full details.
// Create configuration
let configuration = CastleConfiguration(publishableKey: "{Publishable-API-Key}")
configuration.isDebugLoggingEnabled = true // Default false
configuration.isDeviceIDAutoForwardingEnabled = true // Default false
configuration.flushLimit = 10 // Default 20
configuration.maxQueueLimit = 100 // Default 1000
configuration.baseURLAllowList = [ URL(string: "https://api.castle.io/")! ]

// Setup Castle SDK with provided configuration
Castle.configure(configuration)
// Create configuration object
CastleConfiguration *configuration = [CastleConfiguration configurationWithPublishableKey:@"{Publishable-API-Key}"];
configuration.debugLoggingEnabled = YES; // Default false
configuration.deviceIDAutoForwardingEnabled = YES; // Default false
configuration.flushLimit = 10; // Default 20
configuration.maxQueueLimit = 100; // Default 1000
configuration.baseURLAllowList = @[ [NSURL URLWithString:@"https://api.castle.io/"] ];
    
// Setup Castle SDK with provided configuration
[Castle configure:configuration];

Configuration notes

Queue flushing

The SDK queues API calls to save battery life, and it only flushes queued events to the Castle API whenever the app is installed, updated, opened or closed; when identify is called; or when the queue reaches 20 events. This ensures that the device fingerprint is fully profiled before any requests to your server side.

Automatic screen tracking

When automatic screen tracking is enabled, any activities or view controllers shown will automatically generate a screen event. The screen name will use a normalized version of the activities or view controllers title. For more granular control we suggest disabling automatic screen tracking (on by default) and calling screen manually.