This guide explains how to integrate an mtribes Space into your iOS application.
You'll learn how to synchronize changes from your Space into your codebase using our CLI's code generation, and how to use this code to control the UX of your end users.
Additionally you can clone and run a sample application that uses the SDK.
- Xcode 11.0+ / Swift 5.1+
- An iOS project ready to integrate with mtribes
- An mtribes Space with at least one Collection and Experience
- The mtribes CLI installed
- Swift code generated for your Space
Our SDK is designed to use in modern iOS applications with support for backward compatibility, up to devices running iOS 10.0.
Swift is treated as our first class language. The SDK will support Objective C in coming releases.
We support both callbacks and the Combine framework for our asynchronous functionalities. You can use either way to start a session or monitor change events.
With your Space code generated, you're ready to integrate mtribes into your iOS application.
In the code examples which follow, we'll build out an example Space containing:
homepageCollection, which contains:
bodySection, which supports:
The generated code is output to
Don't worry if this isn't clear yet, as it will make more sense after we run through some examples.
iOS SDK integration
We recommend installing our SDK via CocoaPods. We'll be supporting Swift Package Manager and Carthage in the future.
CocoaPods 1.9.0 or newer is required to install a XCFramework.
To install the library using CocoaPods, follow these steps:
- Add the following in your Podfile:
- Then run the following command in the terminal to install dependency:
- Download the latest library from releases and place the framework under your application.
- Select your application project in the Project Navigator and select the application target under the "TARGETS" heading.
- Select the
Generaltab at the top.
- Drag and drop
Frameworks, Libraries, and Embedded Contentsection, and make sure the library is
Embed & Sign.
MTClient is a top level object for configuring your mtribes SDK.
MTClient instance is generated along with your Space integration code and
can be accessed via
Defaults are sensible, but can be overridden to further harmonize mtribes with your application.
Example: Disable session locking to enable Section and Experience change events during a user session.
It's best to configure the Client before starting the session, but configuration can be adjusted at any time.
Before accessing an Experience, it's important to start a session for the active user, so we can serve them personalized states.
We represent an mtribes session via the
session object, which you can access
You can also start a session with Combine framework.
Start a session for an anonymous user.
This should be done:
- on app launch if the user is anonymous
- when a user logs out
It's important to wait for the completion of
before accessing Experiences and Sections.
Logged in user
Starting a session for a logged in user is similar to an anonymous one, except you must provide their unique identifier.
This should be done:
- on app launch if the user is already logged in
- when a user logs in
Again, wait for the completion of
accessing Experiences and Sections.
A user ID should be opaque and not personally identifiable. For example, you should avoid email addresses, social security numbers or similarly personal information.
If you don't have an opaque ID for a user, you can use a secure hashing
algorithm such as SHA256 to encode it
before passing it to
When starting a session, you can also provide fields with values specific to the user. These open up powerful Tribe targeting options in the mtribes platform.
Number contextual fields
It is preferred to use Int, Double, Float or CGFloat rather than Decimal when passing a number contextual field, as you may experience precision lose when using Decimal objects.
Example 1: Start a session for an anonymous user with a contextual properties
Example 2: Start a session for a logged in user with a contextual properties
We currently support the following contextual property types.
Date: ISO-8601 UTC string encoded timestamp e.g.
"2020-12-02T02:45:02.076Z"This must include the date and time.
A maximum of 50 contextual properties can be active at one time. You can remove unused properties to make room for new ones in the contextual property settings page.
To avoid exceeding these limits or sending PII by mistake, we recommend selecting specific contextual properties to include when starting a session.
With the session primed, you can now check whether Experiences modeled in the Space are enabled for a user, and if so, what custom data properties have been defined for them.
Sections contain a list of child Experiences defined at runtime by a Scheduler in the mtribes platform. You can iterate over these child Experiences and render each in turn, depending on its type.
Each Section defines its
Supported Experience types
enum. You can use these
in switch cases and render appropriately.
The state of an Experience or Section may change for a user during their session. You can monitor these changes and reflect them in your code.
You can also monitor change events with the Combine framework.
If you want published change events to fire during a user's session, then you'll
need to set Client.sessionLock to
false. By default session
locking is enabled to avoid published changes negatively impacting UX.
To help you gather user analytical behavioral patterns associated with an
Experience, we expose a
Analytic events support a
Action to help with event
classification. These properties are encoded as a string in the format
<category>/<action>. If only
<action> is provided, a default Category of
user is assumed.
Here are a few examples from our banner Experience above.
Trusted Identity helps ensure the authenticity of users identified into your mtribes Space. View our comprehensive Trusted Identity guide to understand what this is and how to enable it.
Once you have a hashed user signature returned from your server, you should pass this as an option when starting a session for a logged in user.
Using mtribes with Combine
mtribes offers the ability to start session and subscribe to change events using
Start a session using Combine is similar to Start a session with callbacks,
except it returns a custom
Publisher. It is important to wait for the
start before accessing Experiences and Sections.
You can also monitor state changes of an Experience or Section by subscribing to a
You can enable internal SDK logs to gain a deeper understanding of some workflows.
Below is an example of where you can enable the debug log in your app: