Skip to content

SDK Initialization

Basic Configuration

Since the viewDidLoad method of the first displayed view NSViewController and the windowDidLoad method of NSWindowController are called earlier than the AppDelegate's applicationDidFinishLaunching, to avoid abnormal lifecycle collection for the first view, it is recommended to perform SDK initialization in the main.m file.

// main.m file
#import "FTMacOSSDK.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // Local environment deployment, Datakit deployment
        FTSDKConfig *config = [[FTSDKConfig alloc] initWithDatakitUrl:datakitUrl];
        // Using public DataWay deployment
        // FTSDKConfig *config = [[FTSDKConfig alloc] initWithDatawayUrl:datawayUrl clientToken:clientToken];
        config.enableSDKDebugLog = YES;
        [FTSDKAgent startWithConfigOptions:config];
    }
    return NSApplicationMain(argc, argv);
}

Create a main.swift file, and remove @main or @NSApplicationMain from AppDelegate.swift

import Cocoa
import FTMacOSSDK

let delegate = AppDelegate()
NSApplication.shared.delegate = delegate

let config = FTSDKConfig(datakitUrl: datakitUrl)
// Using public DataWay deployment
// let config = FTSDKConfig(datawayUrl: datawayUrl, clientToken: clientToken)
config.enableSDKDebugLog = true
FTSDKAgent.start(withConfigOptions: config)

_ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)
Property Type Required Meaning
datakitUrl NSString Yes Datakit access URL, example: http://10.0.0.1:9529, default port is 9529. The device where the SDK is installed must be able to access this address. Note: Choose one between datakit and dataway configuration
datawayUrl NSString Yes Public DataWay access URL, example: http://10.0.0.1:9528, default port is 9528. The device where the SDK is installed must be able to access this address. Note: Choose one between datakit and dataway configuration
clientToken NSString Yes Authentication token, must be used together with datawayUrl
enableSDKDebugLog BOOL No Sets whether to allow log printing, default is NO
env NSString No Sets the collection environment, default is prod, supports customization. Can also be set via the -setEnvWithType: method using the provided FTEnv enumeration
service NSString No Sets the name of the associated business or service, affecting the service field data in Log and RUM, default is df_rum_macos
globalContext NSDictionary No Adds custom tags. For usage rules, please refer to Custom Tag Usage

User Binding and Unbinding

Usage

/// Bind user information
///
/// - Parameters:
///   - Id: User ID
///   - userName: User name (optional)
///   - userEmail: User email (optional)
///   - extra: User's extra information (optional)
- (void)bindUserWithUserID:(NSString *)Id userName:(nullable NSString *)userName userEmail:(nullable NSString *)userEmail extra:(nullable NSDictionary *)extra;

/// Unbind the current user
- (void)unbindUser;
/// Bind user information
///
/// - Parameters:
///   - Id: User ID
///   - userName: User name (optional)
///   - userEmail: User email (optional)
///   - extra: User's extra information (optional)
open func bindUser(withUserID Id: String, userName: String?, userEmail: String?, extra: [AnyHashable : Any]?)

/// Unbind the current user
open func unbindUser()

Code Example

[[FTSDKAgent sharedInstance] bindUserWithUserID:USERID];
// or
[[FTSDKAgent sharedInstance] bindUserWithUserID:USERID userName:USERNAME userEmail:USEREMAIL];
// or
[[FTSDKAgent sharedInstance] bindUserWithUserID:USERID userName:USERNAME userEmail:USEREMAIL extra:@{EXTRA_KEY:EXTRA_VALUE}];

[[FTSDKAgent sharedInstance] unbindUser];
FTSDKAgent.sharedInstance().bindUser(withUserID: USERID)
// or
FTSDKAgent.sharedInstance().bindUser(withUserID: USERID, userName: USERNAME, userEmail: USEREMAIL)
// or
FTSDKAgent.sharedInstance().bindUser(withUserID: USERID, userName: USERNAME, userEmail: USEREMAIL, extra: [EXTRA_KEY: EXTRA_VALUE])

FTSDKAgent.sharedInstance().unbindUser()

Shutting Down the SDK

Use FTSDKAgent to shut down the SDK.

Usage

/// Shuts down the running objects inside the SDK
- (void)shutDown;
/// Shuts down the running objects inside the SDK
func shutDown()

Code Example

// If dynamically changing SDK configuration, shut down first to avoid generating erroneous data
[[FTSDKAgent sharedInstance] shutDown];
// If dynamically changing SDK configuration, shut down first to avoid generating erroneous data
FTSDKAgent.sharedInstance().shutDown()