SDK Initialization¶
This document covers the initialization and runtime capabilities of the iOS/tvOS/macOS SDK.
Basic Configuration¶
Initialize the SDK in the AppDelegate for iOS/tvOS. For macOS, the viewDidLoad method of the first displayed NSViewController or the windowDidLoad method of NSWindowController is called before applicationDidFinishLaunching in the AppDelegate. To avoid abnormal lifecycle collection of the first view, it is recommended to initialize the SDK in main.m or main.swift.
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
// SDK FTSDKConfig settings
// Local environment deployment, Datakit deployment
//FTSDKConfig *config = [[FTSDKConfig alloc]initWithDatakitUrl:datakitUrl];
// Using public network DataWay deployment
FTSDKConfig *config = [[FTSDKConfig alloc]initWithDatawayUrl:datawayUrl clientToken:clientToken];
//config.enableSDKDebugLog = YES; //debug mode
config.compressIntakeRequests = YES;
//Start SDK
[FTMobileAgent startWithConfigOptions:config];
//...
return YES;
}
// main.m file
#import <Cocoa/Cocoa.h>
#import <TrueWatchSDK/TrueWatchSDK.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Local environment deployment, Datakit deployment
FTSDKConfig *config = [[FTSDKConfig alloc] initWithDatakitUrl:datakitUrl];
// Using public network DataWay deployment
// FTSDKConfig *config = [[FTSDKConfig alloc] initWithDatawayUrl:datawayUrl clientToken:clientToken];
config.enableSDKDebugLog = YES;
[FTSDKAgent startWithConfigOptions:config];
}
return NSApplicationMain(argc, argv);
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// SDK FTSDKConfig settings
// Local environment deployment, Datakit deployment
//let config = FTSDKConfig(datakitUrl: url)
// Using public network DataWay deployment
let config = FTSDKConfig(datawayUrl: datawayUrl, clientToken: clientToken)
//config.enableSDKDebugLog = true //debug mode
config.compressIntakeRequests = true //Compress reported data
FTMobileAgent.start(withConfigOptions: config)
//...
return true
}
Create a main.swift file, remove @main or @NSApplicationMain from AppDelegate.swift.
import Cocoa
import TrueWatchSDK
let delegate = AppDelegate()
NSApplication.shared.delegate = delegate
let config = FTSDKConfig(datakitUrl: datakitUrl)
// Using public network DataWay deployment
// let config = FTSDKConfig(datawayUrl: datawayUrl, clientToken: clientToken)
config.enableSDKDebugLog = true
FTSDKAgent.start(withConfigOptions: config)
_ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)
Initialization Configuration Naming
In SDK 1.6.6 and above, it is recommended to use FTSDKConfig for new code. FTMobileConfig is still compatible and inherits from FTSDKConfig. For versions below 1.6.6, continue using FTMobileConfig.
| Property | Type | Required | Description |
|---|---|---|---|
| datakitUrl | NSString | Yes | Local environment deployment (Datakit) report URL address. Example: http://10.0.0.1:9529. The default port is 9529. The device installing the SDK must be able to access this address. Note: Only one of datakitUrl and datawayUrl should be configured |
| datawayUrl | NSString | Yes | Public network DataWay report URL address. Obtain from [User Access Monitoring] application. Example: https://open.dataway.url. The device installing the SDK must be able to access this address. Note: Only one of datakitUrl and datawayUrl should be configured |
| clientToken | NSString | Yes | Authentication token, must be used together with datawayUrl |
| enableSDKDebugLog | BOOL | No | Set whether to allow printing logs. Default NO |
| env | NSString | No | Set the collection environment. Default prod. Supports customization, or use the provided FTEnv enum with the -setEnvWithType: method |
| service | NSString | No | Set the business or service name. Affects the service field in Log and RUM data. Defaults: iOS: df_rum_ios, tvOS: df_rum_tvos, macOS: df_rum_macos |
| globalContext | NSDictionary | No | Add custom tags. See here for adding rules |
| groupIdentifiers | NSArray | No | Array of AppGroups Identifiers for iOS Widget Extensions that need data collection. If enabling Widget Extension data collection, you must set App Groups and configure the Identifier in this property. macOS does not support Widget Extension |
| autoSync | BOOL | No | Whether to automatically sync data to the server after collection. Default YES. When NO, use [[FTMobileAgent sharedInstance] flushSyncData] for iOS/tvOS, or [[FTSDKAgent sharedInstance] flushSyncData] for macOS to manage data synchronization manually |
| syncPageSize | int | No | Set the number of items per sync request. Range: [5,). Note: larger values consume more computing resources for data sync. Default is 10 |
| syncSleepTime | int | No | Set the sync interval. Range: [0,5000]. Default is not set |
| enableDataIntegerCompatible | BOOL | No | Recommended to enable when coexisting with web data. This configuration handles data type storage compatibility for web data |
| compressIntakeRequests | BOOL | No | Compress upload sync data with deflate. Supported in SDK 1.5.6 and above. Default is off |
| enableLimitWithDbSize | BOOL | No | Enable using DB to limit total cache size. Note: When enabled, FTLoggerConfig.logCacheLimitCount and FTRUMConfig.rumCacheLimitCount will be invalid. Supported in SDK 1.5.8 and above |
| dbCacheLimit | long | No | DB cache limit size. Range: [30MB,). Default is 100MB, in bytes. Supported in SDK 1.5.8 and above |
| dbDiscardType | FTDBCacheDiscard | No | Set the data discard rule in the database. Default FTDBDiscard. FTDBDiscard discards newly appended data when the count exceeds the maximum; FTDBDiscardOldest discards old data when the count exceeds the maximum. Supported in SDK 1.5.8 and above |
| dataModifier | FTDataModifier | No | Modify individual fields. Supported in SDK 1.5.16 and above. See Data Collection Masking for usage examples |
| lineDataModifier | FTLineDataModifier | No | Modify a single data line. Supported in SDK 1.5.16 and above. See Data Collection Masking for usage examples |
| enableDataFilter | BOOL | No | Whether to enable SDK-side DataKit compatible data filtering, including local and remote filtering rules. Default YES. Supported in SDK 1.6.4 and above. See Data Filter for usage examples |
| dataFilters | NSDictionary | No | App-local data filtering rules. Supported categories: logging, rum. Supported in SDK 1.6.4 and above. See Blacklist Rules for rule syntax |
| remoteConfiguration | BOOL | No | Whether to enable remote configuration for data collection. Default is off. When enabled, SDK initialization or app warm start triggers data update. Supported in SDK 1.5.17 and above. Datakit version >=1.60 or using public network DataWay required |
| remoteConfigMiniUpdateInterval | int | No | Set the minimum update interval for remote dynamic configuration, in seconds. Default is 12 hours. Supported in SDK 1.5.17 and above |
| remoteConfigFetchCompletionBlock | FTRemoteConfigFetchCompletionBlock | No | Callback for remote configuration fetch result, allows custom adjustment of the configuration model. Supported in SDK 1.5.19 and above. See here for usage examples |
Blacklist Filtering¶
Data Filter is used to filter RUM and Log data according to rules before the SDK writes to the local cache. Data that hits the filtering rules will not enter the local cache and will not be reported.
- Local rules: Configured via
FTSDKConfig.dataFilters, delivered by the app during SDK initialization. -
Remote rules: After enabling
FTSDKConfig.enableDataFilter, the SDK will pull the blacklist rules added on the Studio side. -
Local and remote rules take effect simultaneously. If any rule is hit, the data item will be discarded.
- Blacklist filtering occurs after
lineDataModifierand before writing to the local cache. If bothlineDataModifierand blacklist filtering are configured, the filtering rules will evaluate based on the modified data.
Data Filter acts on the SDK data writing pipeline. Too many rules or overly complex regular expressions may affect data writing performance. It is recommended to configure only necessary rules.
Rule Syntax¶
The Data Filter rule syntax is basically the same as the blacklist filtering rule syntax. For the complete syntax description, refer to Blacklist Filtering Rules.
The key of dataFilters indicates the data category. The SDK currently supports:
| Category | Description |
|---|---|
logging |
Log data |
rum |
RUM data |
Each rule is represented by { condition }. If any rule is hit, the data under that category is filtered. The tag and field fields of the data can be used in the rules.
For field value format and operator semantics, refer to Field Value Format Description and Operator Description in the blacklist filtering rules.
All field values in the SDK rule string must use array format, and reverse operators are fixed as notin and notmatch.
User Binding and Unbinding¶
Use FTMobileAgent to bind user information and unbind the current user.
/// Bind user information. Call this method after successful user login to bind user information.
///
/// - Parameters:
/// - Id: User ID
/// - userName: User name (optional)
/// - userEmail: User email (optional)
/// - extra: Additional user information (optional)
- (void)bindUserWithUserID:(NSString *)Id userName:(nullable NSString *)userName userEmail:(nullable NSString *)userEmail extra:(nullable NSDictionary *)extra;
/// Unbind the current user. Call this method after user logout to unbind user information.
- (void)unbindUser;
/// Bind user information. Call this method after successful user login to bind user information.
///
/// - Parameters:
/// - Id: User ID
/// - userName: User name (optional)
/// - userEmail: User email (optional)
/// - extra: Additional user information (optional)
open func bindUser(withUserID Id: String, userName: String?, userEmail: String?, extra: [AnyHashable : Any]?)
/// Unbind the current user. Call this method after user logout to unbind user information.
open func unbindUser()
For extra adding rules, please refer to here.
Runtime Capabilities¶
Shutting Down the SDK¶
When using FTMobileAgent to shut down the SDK, must be called on the main thread, otherwise thread safety issues may occur. If dynamically changing the SDK configuration, you need to shut it down first to avoid incorrect data generation.
Clearing SDK Cache Data¶
Use FTMobileAgent to clear unreported cached data.
Actively Syncing Data¶
Use FTMobileAgent to actively sync data.
Only when
FTSDKConfig.autoSync = NO, you need to perform data synchronization manually. The old versionFTMobileConfig.autoSyncis still compatible.
Actively Syncing Dynamic Configuration¶
The runtime dynamic configuration capabilities have been moved to Dynamic Configuration.