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
.
Create your config file 📄
Create a JSON config file to your XCode project, starting with a template:
Create a copy of of the file
cmDevConfig.json
template file below, replacingYOUR_BUNDLE_ID
with your real bundle ID. This template includes a simple launch message which will help your verify the integration is working.Add the file to your Xcode project. Ensure you check the "Target Membership" box for your app.
In Xcode open "Project Settings" > "Build Phases" > "Copy Bundle Resources", and verify the file you just added to the project is listed. If not, add it!
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 changed the name if your config file in the prior stepThe 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:
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
Startup work will be dispatched to background threads, and will not impact your app's startup time.
Test the integration 🧪
Launch your app! If the integration is working, you should get an alert after startup driven by the template config file. It will tell you if your app was launched in landscape or portrait, and rotating your device/simulator then re-launching should give the opposite result.
Log important events (optional, recommended) 🎪
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.
Add custom properties (optional, recommended) 📅
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.
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:
In Xcode > project editor > Signing & Capabilities > Add (+) > Background Modes check the boxes for "Background fetch" and "Background processing". See Apple guide.
In your
Info.plist
add an array "Permitted background task scheduler identifiers" (BGTaskSchedulerPermittedIdentifiers
) with these two values:io.criticalmoments.bg_fetch
andio.criticalmoments.bg_process
. See Apple guide.
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!
Request Notification Permissions 📬
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.
Update Your Info.plist 📔
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".
Try a Demo Inside Your Own App 🤖
Update your cmDevConfig.json
with the config below. Be sure to replace YOUR_APP_ID
in the json with your app ID. Each time your app launches, the demo will launch.
This demo is implemented entirely as a CM config file! Feel free to read any part to see how it works. Revert the demo config content when you are done with the demo.
Start building 🏗️
Remove the demo actions and triggers from your copy of cmDevConfig.json
and start building using the powerful combination of conditionals, events, properties, feature flags, and actions!
Some good places to start and get ideas:
Read our Concepts Overview to understand all the tools at your disposal.
Try our Demo App for an interactive demo of conditionals, themes, and actions.
Last updated