目录
此内容是否有帮助?

# iOS

::: Tips

Before you begin, please read Preparation before Data Ingestion (opens new window)

The lowest system version required by iOS SDK is iOS 8.0

The size of iOS SDK (Framework format) is around 8.1 MB

:::

Latest version: 2.8.3

Update time: November 08, 2022

Resource download: Framework (opens new window)Source Code (opens new window)

# 1. SDK Integration

# 1.1 Automatic Integration

  • CocoaPods
  1. Create a Podfile in your Xcode project directory by running pod init in your terminal.

Edit the generated Podfile , and add the following lines:

platform :ios, '9.0'
target 'YourProjectTarget' do
  pod 'ThinkingSDK'  #ThinkingSDK
end
  1. Navigate to your project's root folder and run pod install in your terminal

After success, the following tip will appear in the terminal:

  1. Open project

After the command is executed successfully, .xcworkspace file will be generated, indicating that you have successfully imported the iOS SDK. Open the .xcworkspace file to start the project (note: the .xcodeproj file cannot be opened at the same time)

  • Carthage
  1. Add configuration to the Cartfilefile: github "ThinkingDataAnalytics/ios-sdk
  2. Execute carthage update --platform iOS and add ThinkingSDK.frameworkto your project

# 1.2 Manual Integration

  1. Download and unzip the iOS SDK (opens new window)

  2. Add ThinkingSDK.framework into the Xcode project workspace project

  3. In Xcode, go to Targets > Build Settings, add line -ObjC to Other linker flags :

  4. In Xcode, go to Targets > Build Phases, add the following dependencies into Link Binary With Libraries: libz.dylib, Security.framework, SystemConfiguration.framework, libsqlite3.tbd

# 2. Initialization

Instruction on parameters:

  • APPID: The APPID of your project, which can be found on the project management page of the TE.
  • SERVER_URL:
    • If you are using a SaaS version, please check the receiver URL on this page

    • If you use the private deployment version, you can customize the data tracking URL .

# 3. Common Features

We suggested that you read User identification rules (opens new window) before using common features; SDK would generate a random number that would be used as the distinct ID, and save the ID locally. Before the user logs in, the distinct ID would be used as the identification ID. Note: The distinct ID would change after the user reinstalled the App or used the APP with a new device.

# 3.1 Login

When the users log in , login could be called to set the account ID of the user. TE would use the account ID as the identification ID, and this ID would be saved before logout is called. The previous account ID would be replaced if login has been called multiple times.

Login events wouldn't be uploaded in this method.

# 3.2 Super Properties

Super properties refer to properties that each event might have. You can call setSuperProperties to set super properties. It is recommended that you set super properties first before sending data. Some important properties (e.g., the membership class of users, source channels, etc.) should be set in each event. At this time, you can set these properties as super properties.

Super properties would be saved in local storage, and will not need to be called every time the APP is opened. If the super properties set previously are uploaded after calling setSuperProperties, previous properties would be replaced.

  • Key is the name of the property and refers to the string type. It must start with a character, and contain numbers, characters (insensitive to case, and upper cases would be transformed into lower cases by TE) and underscores "_", with a maximum length of 50 characters.
  • Value, the value of the property, supports string, numbers, Boolean, time, object, array object, and array

The requirements for event properties and user properties are the same with that for super properties

# 3.3 Automatically Track Events

The following code is an example of tracking installation, open_app and close_app events. To get more information about the automatic tracking of SDK, please refer to the Detailed introduction of automatic tracking function

// enable autotrack event
[[ThinkingAnalyticsSDK sharedInstanceWithAppid:APP_ID]enableAutoTrack:ThinkingAnalyticsEventTypeAppInstall | ThinkingAnalyticsEventTypeAppStart |ThinkingAnalyticsEventTypeAppEnd];

# 3.4 Sending Events

You can call track to upload events. It is suggested that you set event properties based on the data tracking plan drafted previously. Here is an example of a user buying an item:

The event name is string type. It could only start with a character and could contain figures, characters, and an underline "_", with a maximum length of 50 characters.

# 3.5 User Properties

You can set general user properties by calling user_set API. The original properties would be replaced by the properties uploaded via this API. If no user properties are set before, user properties would be created. The data type of newly-created user properties must be the same as the uploaded properties. User name setting is taken as the example here:

# 4. Best Practice

The following sample code covers all the above-mentioned operations. It is recommended that the SDK be used in the following steps:

ThinkingAnalyticsSDK *instance;
if (privacy policy is authorized) {
    instance = ThinkingAnalyticsSDK.sharedInstance(this, TE_APP_ID, TE_SERVER_URL);
    if(instance != null)
    {
      
      //if the user has logged in, the account ID of the user could be set as the unique identifier 
      instance.login("TE");
      
      //After setting super properties, each event would have super properties
      NSMutableDictionary *superProperties = [NSMutableDictionary new];
      [superProperties setValue:@"te" forKey:@"channel"]; //string
      [superProperties setValue:@1 forKey:@"age"]; //number
      [superProperties setValue:@YES forKey:@"isSuccess"]; //boolean
      [superProperties setValue:[NSDate now] forKey:@"birthday"]; //time
      [superProperties setValue:@{@"key":@"value"} forKey:@"object"]; //object
      [superProperties setValue:@[@{@"key":@"value"}] forKey:@"object_arr"]; //array object
      [superProperties setValue:@[@"value"] forKey:@"arr"]; //array
      [instance setSuperProperties:superProperties]; //set super properties
      
      //enable autotrack event
      [[ThinkingAnalyticsSDK sharedInstanceWithAppid:APP_ID]enableAutoTrack:ThinkingAnalyticsEventTypeAppInstall | ThinkingAnalyticsEventTypeAppStart |ThinkingAnalyticsEventTypeAppEnd];
      
      //send event
      NSDictionary *eventProperties = @{ @"product_name": @"product name"};
      [instance track:@"product_buy" properties:eventProperties];
      
      //Set user properties
      [instance user_set:@{@"username": @"TE"}];
    }
}