Castled SDK allows you to show in-app messages to users who are currently engaging with your app. These messages are typically used to communicate important information, deliver targeted promotions, provide user guidance, or engage with users within the app.

Enabling In-App messages in SDK

  1. Set the flag enableInApp to true when initializing the SDK
@main
class AppDelegate: UIResponder, UIApplicationDelegate , UNUserNotificationCenterDelegate {
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let config = CastledConfigs.sharedInstance

        //Mandatory : enableInApp needs to be mandatorily set to true for in app support.
        config.enableInApp = true

        //Optional : Required to enable background task to monitor the app is active
        //Default value is false.
        config.permittedBGIdentifier = "com.castled.backgroundtask"

        //Optional : Frequency at which the heart beat check happens.
        //default is 15 minutes if permittedBGIdentifier is configured.
        config.inAppFetchIntervalSec = <value in seconds>
        
        Castled.configure(registerIn: application, 
            launchOptions: launchOptions, 
            instanceId: "mytestapikey", 
            delegate: self)
        
        return true
    }
}
  1. User Registration

Once the user registration is complete, the app should invoke the registerUser API in Castled SDK to register the user and the device token with the backend service.App developer to make sure the below API is called at the appropriate time in the sequence of events.

Castled.registerUser(userId: '<userId>', apnsToken: '<device-token>')

Triggering In-App SDK events

In-App messages are typically shown in response to events that happen within your app. You can decide the trigger event for in-app display when creating the campaign. By default SDK tracks app_opened and page_viewed events. Note that these events are not reported to our sever and is used only to determine if an in-app needs to be shown or not.

If you want to use a custom event as the trigger event, SDK provides a method to raise such an event based on a user action within your app.

Castled.logCustomAppEvent(context : UIViewController,eventName : String ,params : eventParams,showLog : Bool? = true)
  • eventName : name of the custom event.
  • params : event parameters as a key value pair
  • showLog : a flag to enable showing the log of the event

For e.g. If your shopping app wants to display an in-app message when value of items in cart greater than a certain number, app would log a custom event as follows whenever user adds an item to cart.

// Name of event: added_to_cart
// Event params:
//     value: total value of items in cart
//     items: total number of items in cart
Castled.logCustomAppEvent(
            context,
            "added_to_cart",
            ["value" : 1400, "items" : 10 , "currency" : "USD"],
            true)

SDK will then make sure of displaying the eligible in-app to user if there is one.