Search…
4. Usage
You can use the SDK calls in your App code in both Swift and Objective-C. If your app is written in Swift, You will need to add a bridging header (if you don't have one already in your project).
Import SENTSDK public header in your bridging header file to access the SDK:
1
@import SENTSDK;
Copied!
You will also need to setup Location Authorization Permission before initializing the SDK. The Sentiance SDK does not handle location authorizations but needs it to function. Add the necessary code in your app following the Apple guidelines for Location Services Authorizations before initializing the SDK.
In the didFinishLaunchingWithOptions method of your AppDelegate, create a SENTConfig object with your Sentiance app ID and secret.
Swift
Objective-C
1
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
2
let config = SENTConfig(appId: self.appId, // Add yours.
3
secret: self.secret, // Add yours.
4
launchOptions: launchOptions)
5
}
Copied!
1
@import SENTSDK;
2
3
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
4
SENTConfig *conf = [[SENTConfig alloc] initWithAppId:@"APPID"
5
secret:@"SECRET"
6
link:userLinker
7
launchOptions:launchOptions];
8
}
Copied!
If you don't have an appID and secret key yet, read these instructions.
In the above example, we hard-code the the appID and secret key for testing purposes. However, this is not secure and can lead to leaked credentials. In your own app, load these credentials from a secure source such a remote server, and store them securely on the device.
SENTConfig accepts a userLinker which is responsible for handling user linking. The linker is invoked by the SDK when creating a Sentiance user (during the first initialization) to give you a chance to link your app's user to the Sentiance user. If linking succeeds, this linker does not get invoked during subsequent SDK intializations, unless the SDK gets reset.
Objective-C
1
MetaUserLinker userLinker = ^(NSString *installId, void (^linkSuccess)(void), void (^linkFailed)(void)) {
2
// Supply the 'installId' to your server, which should then initiate
3
// a user linking request with the Sentiance backend.
4
5
[self requestLinking:installId completion:^(BOOL success) {
6
if (success) {
7
linkSuccess(); // Call if linking succeeds
8
} else {
9
linkFailed(); // Call if linking fails
10
}
11
}];
12
};
Copied!
You can learn more about user linking here.

Optional: SDK Status update handler

This step is optional, but indispensable if you want to be kept up-to-date with changes to the SDK status.
Swift
Objective-C
1
config.didReceiveSdkStatusUpdate = { status in
2
}
Copied!
1
[conf setDidReceiveSdkStatusUpdate:^(SENTSDKStatus *status) {
2
}];
Copied!

Initializing the SDK

Until the SDK is properly initialized, none of the methods in the SDK will work, with the exception of sharedInstance, initWithConfig and getInitState.
In the project's AppDelegate file,
Swift
Objective-C
1
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
2
// Setup Config.
3
let config = SENTConfig(appId: self.appId, // Add yours.
4
secret: self.secret, // Add yours.
5
link: userLinker // If user linking.
6
launchOptions: launchOptions)
7
8
// Initialize SDK.
9
SENTSDK.sharedInstance().initWith(config,
10
success: { // Successful Initialization
11
// Add code to start service.
12
self.startService()
13
},
14
failure: { issue in
15
print("Failed to Initialize SDK with issue: \(issue)")
16
})
17
}
Copied!
1
@import SENTSDK;
2
3
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
4
    // Creation of SENTConfig here, see previous step
5
SENTConfig *conf = [[SENTConfig alloc] initWithAppId:@"APPID"
6
secret:@"SECRET"
7
link:userLinker
8
launchOptions:launchOptions];
9
10
// Initialize SDK
11
    [[SENTSDK sharedInstance] initWithConfig:conf success:^{
12
13
    } failure:^(SENTInitIssue issue) {
14
15
    }];
16
17
    return YES;
18
}
Copied!
  1. 1.
    In the didFinishLaunchingWithOptions method of your AppDelegate, pass the SENTConfig you created in the previous step to the initWithConfig method of the Sentiance SDK.
  2. 2.
    Additionally, success and failure blocks must be passed as well, which will inform you when initialization has succeeded or failed.
The initWithConfig:call must be executed before didFinishLaunchingWithOptions: returns. Therefore, you must call it synchronously on the main UI thread. If you plan to add a remote flag to control the initialization (e.g. Firebase Remote Config), make sure the check is synchronous (e.g. using a cached flag).
See here to understand more about why this is important.

Starting Detections

We recommend that SDK startup is done within didFinishLaunchingWithOptions of AppDelegate file to ensure that there are no unintended side effects.
Call the start method from the success block of the initWithConfig method:
Swift
Objective-C
1
SENTSDK.sharedInstance().start { status in
2
if let status = status {
3
switch status.startStatus {
4
case SENTStartStatus.notStarted:
5
print("SDK Status: Not started")
6
case SENTStartStatus.pending:
7
print("SDK Status: Pending")
8
case SENTStartStatus.started:
9
print("SDK Status: Started")
10
case SENTStartStatus.expired:
11
print("SDK Status: Expired")
12
@unknown default:
13
print("Status unknown")
14
}
15
}
16
}
Copied!
1
[[SENTSDK sharedInstance] start:^(SENTSDKStatus *status) {
2
    if ([status startStatus] == SENTStartStatusStarted) {
3
        // SDK started properly.
4
    } else if ([status startStatus] == SENTStartStatusPending) {
5
        // Something prevented the SDK to start properly (check the location permission). Once fixed, the SDK will start automatically.
6
    }else {
7
        // SDK did not start.
8
    }
9
}];
Copied!
You can check the status to see whether starting has succeeded correctly or not.

Overall Code

Swift
Objective-C
1
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
2
// Handle Location permissions before-hand.
3
4
// Setup Config.
5
let config = SENTConfig(appId: "", // Add yours.
6
secret: "", // Add yours.
7
link: userLinker // If user linking.
8
launchOptions: launchOptions)
9
10
// Initialize SDK.
11
SENTSDK.sharedInstance().initWith(config,
12
success: {
13
// Start the SDK
14
SENTSDK.sharedInstance().start { status in
15
// Handle the start status.
16
}
17
},
18
failure: { issue in
19
print("Failed to Initialize the SDK due to issue: \(issue)")
20
})
21
return true
22
}
Copied!
1
@import SENTSDK;
2
3
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
4
    // Creation of SENTConfig here, see previous step
5
SENTConfig *conf = [[SENTConfig alloc] initWithAppId:@"APPID"
6
secret:@"SECRET"
7
link:userLinker
8
launchOptions:launchOptions];
9
10
// Initialize SDK
11
    [[SENTSDK sharedInstance] initWithConfig:conf success:^{
12
13
        // SDK Start
14
    [[SENTSDK sharedInstance] start:^(SENTSDKStatus *status) {
15
            if ([status startStatus] == SENTStartStatusStarted) {
16
                // SDK started properly.
17
            } else if ([status startStatus] == SENTStartStatusPending) {
18
                // Something prevented the SDK to start properly (check the location permission). Once fixed, the SDK will start automatically.
19
            }else {
20
            // SDK did not start.
21
            }
22
        }];
23
24
    } failure:^(SENTInitIssue issue) {
25
26
    }];
27
28
    return YES;
29
}
Copied!