Skip to content

iOS Client Integration

Step 1: Set your License Key

After you installed ContextSDK, you need to add your license key. Register here to get started.

In your willFinishLaunchingWithOptions: (or anywhere before you access the SDK) setup the ContextManager:

AppDelegate.swift
import ContextSDK
AppDelegate.swift willFinishLaunchingWithOptions:
ContextManager.applicationDidFinishLaunchingWithOptions(launchOptions, licenseKey: "YOUR_LICENSE_KEY")
  1. Create a new custom Swift class named AppDelegate.swift
    AppDelegate.swift
    import ContextSDK
    
    class AppDelegate: NSObject, UIApplicationDelegate {
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
            ContextManager.applicationDidFinishLaunchingWithOptions(launchOptions, licenseKey: "YOUR_LICENSE_KEY")
            return true
        }
    }
    
  2. In your App scene UIApplicationDelegateAdaptor property wrapper
    struct YourApp: App {
        @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
        // ...
    }
    

Note: Android is still in beta - to obtain a license key contact us at support@contextsdk.com

  1. In your Application sub-class call the following code:
class MainApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        ContextSDK.setup(this, "YOUR_LICENSE_KEY_HERE")
    }
}
  1. In your primary Activity subclass, or in every Activity in your application, allow ContextSDK to attach and detach:
class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    ContextSDK.attachToActivity(this)
  }

  override fun onDestroy() {
    super.onDestroy()
    ContextSDK.detachFromActivity(this)
  }
}
  1. In your Application sub-class call the following code:
public class MainApplication extends Application {

  @Override
  public void onCreate() {
    super.onCreate();
    ContextSDK.Companion.setup(this, "YOUR_LICENSE_KEY_HERE", new ContextSDKConfiguration());
  }
}
  1. In your primary Activity subclass, or in every Activity in your application, allow ContextSDK to attach and detach:
public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ContextSDK.Companion.attachToActivity(this);
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    ContextSDK.Companion.detachFromActivity(this);
  }
}

Notes:

  • If the above isn't setup correctly, ContextSDK won't be able to start and stop collection of accelerometer & gyroscope data.
  • The Activity must conform to LifecycleOwner (e.g. AppCompatActivity). Plain Activities are not supported.

Call the following code on app start:

import { setup } from 'react-native-context-sdk';

void setup("YOUR_LICENSE_KEY_HERE");

In the Start() of a MonoBehaviour that is run early in your game (or anywhere before you access the SDK) setup the ContextManager:

InitContextSDK.cs
using static ContextSDKBinding;

public class InitContextSDK : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        ContextSDKBinding.SetupWithAPIBackend("YOUR_LICENSE_KEY");
    }
}

Call the following code on app start:

import 'package:context_sdk/context_sdk.dart';

final _contextSdkPlugin = ContextSdk();

_contextSdkPlugin.setup("YOUR_LICENSE_KEY_HERE");

Step 2: Import ContextSDK

AppDelegate.swift
import ContextSDK

Step 3: Ensure Remote Notification entitlement

In your Xcode project, go to Signing & Capabilities and enable the Remote Notification capability in the Background Modes section.

Step 4: Provide User ID and Push Token

You need to register the user's device with a unique user ID and the device's push token. The user-id may be whatever you use to identify each user in your existing push notification system, e.g. an email address or a user ID from your database.

public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
  // Your existing code for handling device token registration

  // Set the device token and user ID with ContextSDK to enable push notifications
  ContextPush.setDeviceToken(deviceToken).setUserId(userId)
}
public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
  // Your existing code for handling device token registration

  // Set the device token with ContextSDK to register the device for push notifications
  ContextPush.setDeviceToken(deviceToken)
}
// Set the user ID once it is available to associate notifications with the correct user
ContextPush.setUserId(userId)

Step 5: Handle Background Notifications

Ensure that your app can handle push notifications received while in the background by implementing the following method:

func application(_ application: UIApplication,
                didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
  // ContextSDK will process the notification if it is a valid ContextPush notification
  // The completion handler will be automatically called if processed
  if ContextPush.applicationDidReceiveRemoteNotification(userInfo, fetchCompletionHandler: completionHandler) {
      return
  }

  // Handle other types of background notifications with your existing code, if needed
}

Step 6: Handle Notification Opens

Track when users open notifications to gather engagement metrics and improve user experience:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
    // Process the notification with ContextSDK to track open rates and user interactions
    ContextPush.userNotificationCenterDidReceiveResponse(response)

    // Your existing code
    // Ensure completion handler is called to finalize notification handling
    completionHandler()
}

Next Step: Push Provider Integration

After setting up your iOS client, integrate with your existing push provider:

  OneSignal

  Customer.io

  Simple Web Request

  More to come