Quick Start

Get up and running! ๐Ÿ‘Ÿ๐Ÿ‘Ÿ

Let's get you started! This guide will walk you through the most important steps

Install the SDK ๐Ÿ”ง

Our iOS SDK is distributed as as a SPM package (Swift Package Manager). Simply add a SPM dependency in Xcode on https://github.com/criticalmoments/criticalmoments.

Detailed steps for adding SPM package
  • Open your app project in Xcode. Click "File" > "Add Package Dependencies..."

  • Paste the following URL in the search box in the top right: https://github.com/criticalmoments/criticalmoments and select the Critical Moments package. We suggest leaving the default settings for dependency rules: (Branch=main). We only merge stable releases to our main branch.

  • Select your iOS app project in the "Add to Project..." dropdown

  • Click "Add Package"

  • Select your iOS app target in the "Add to Target" dropdown

  • Click "Add Package"

Create your config file ๐Ÿ“„

The CM config file defines how the SDK integrates into your app. Let's get started with a template integration:

  • Download our template cmDevConfig.json (link), which includes a demo of our features on launch. We'll remove the demo later, but it's helpful to test the integration.

  • Set the appId on the 3rd line to your app's bundle ID.

  • Add the file to your Xcode project.

Integrate the SDK into your app ๐Ÿงฉ

Add the following code snippet into your app.

  • Put it somewhere that will run right after app startup, such as AppDelegate.didFinishLaunchingWithOptions

  • Replace the YOUR_API_KEY string an API key you created in your account.

  • Replace "cmDevConfig.json" if you used a different name for the config file.

  • The console will show a warning for now, but this will be resolved in a later step of this guide

In your App Delegate imports add:

import CriticalMoments

Add the following code to your app launch. The location depends on the type of app you have:

  • App Delegate based apps, call from application(_:didFinishLaunchingWithOptions:)

  • SwiftUI lifecycle apps, call from YourApp.init

CriticalMoments.shared().setApiKey("YOUR_API_KEY", error: nil)
CriticalMoments.shared().setDevelopmentConfigName("cmDevConfig.json")
// We'll add this later. This must be set before releasing app.
// CriticalMoments.shared().setReleaseConfigUrl("YOUR_URL")
CriticalMoments.shared().start()

Startup work will be dispatched to background threads, and will not impact your app's startup time.

Critical Moments requires a few updates to your Info.plist file. We've made a script to make this easy. Run the following command in your project folder in terminal, and then follow the instructions. It will explain the changes it's making, and won't make any changes until you confim:

bash <(curl -fsSL https://raw.githubusercontent.com/CriticalMoments/CriticalMoments/refs/heads/vnext/ios/tools/setup_tool/setup_cm.sh)
Manual Instructions

Don't like magic scripts? We get that. You can also update the Info.plist manually.

Part 1: Enable Processing For Smart Notifications

Features like smart notifications require running your app to run in the background for brief periods, to check for ideal conditions. To setup background work:

  1. In Xcode > project editor > Signing & Capabilities > Add (+) > Background Modes check the boxes for "Background fetch" and "Background processing". See Apple guide.

  2. In your Info.plist add an array "Permitted background task scheduler identifiers" (BGTaskSchedulerPermittedIdentifiers) with these two values: io.criticalmoments.bg_fetch and io.criticalmoments.bg_process . See Apple guide.

Part 2: Set Bluetooth Reason String

Some optional properties like has_bt_headphones and bluetooth_permission use the system's bluetooth APIs. These won't be called unless you include them in your config. However Apple will detect them, and want a description in the Info.plist file.

Add an entry for NSBluetoothAlwaysUsageDescription to your Info.plist describing your bluetooth usage. If you app uses bluetooth directly, keep the description you have. If you don't otherwise use bluetooth, add a general description, such as "Used to show messages when not using peripherals".

Test the Integration ๐Ÿงช

Launch your app. If the integration is working, you should see a CM demo when the app starts; this demo was included in the template file you downloaded earlier.

If there are any issues, check the logs. When running a debug build, Critical Moments will log any issues preventing the SDK from working.

Note: while CM now works in debug mode, more steps below are needed before releasing to the App Store.

Try out the demo to see each feature of the SDK integrated into your app. To re-lauch the demo, simply restart the app.

Once your done with the demo, you can delete the demo content in cmDevConfig.json; just be sure leave the configVersion and appId json entries.

This demo is implemented entirely in config

It shows how you can add many features to your app without writing code, including fully native UI, integrations into native APIs, powerful conditions, and notifications. If you're curious how any part of the demo works, simply read cmDevConfig.json.

In a later step, we'll setup your app to update this config over-the-air, without app updates.

The more Critical Moments knows about your user's behaviours, the better you can target and optimize with Critical Moments.

We recommend you log important events, including the important actions your users performs in app. See the events documentation for the suggested events and details.

CriticalMoments.shared().sendEvent("completed_game_level")

Properties are datapoints used in targeting. The more useful data you add, the more powerful the targeting system can be.

We recommend you add the important properties like account_creation_date, has_subscription, and others. See the custom properties documentation for suggested custom properties, and API reference.

try? CriticalMoments.shared().setIntegerProperty(42, forKey: "max_game_level")

Setup a cloud config file โ˜๏ธ

For local development you can use your local JSON config file built into the app binary. However, production builds must use a signed config file, hosted on the web.

Read the Remote Control Docs for the best practices of picking a hosting provider, signing, and deploying your config.

Once deployed, set the release config URL in your App Delegate, and uncomment the call to setReleaseConfigUrl("YOUR_URL").

You can test your app using the production config by running a "release" build instead of a "debug" build in XCode.

Update Anytime

Deploying the config file can be done outside of app releases, allowing you to update your apps behaviour without waiting for app reviews, or users to update their builds!

If you ever want to show the user notifications, you'll need to ask the user for permission first.

Requesting permission with the Critical Moments helper method instead of the system requestAuthorization method is beneficial as it will schedule any CM notifications after approval. An optional callback will be called after the user approves/denies notifications.

CriticalMoments.shared().requestNotificationPermission()

Customize your Integration ๐Ÿ—๏ธ

Remove the demo content from your copy of cmDevConfig.json and start building using the powerful combination of conditionals, notifications, events, properties, feature flags, and actions!

Some good places to start and get ideas:

Last updated