Simple Asset Tracking Example

This example guides you through the following steps:

  • Initialization with Customized Configurations: Learn how to set up the SDK with customized configuration values, tailoring it to your specific requirements and preferences.

  • Updating Configuration Settings at Runtime: Discover how to dynamically modify configuration settings such as LocationConfig, NotificationConfig, and DataTrackingConfig during the execution of your application. This flexibility empowers you to adapt the SDK to changing conditions and user needs in real time.

For all code examples, refer to iOS Tracking Android Code Examples

UpdateConfigurationViewController view source

1
class UpdateConfigurationViewController: UIViewController {
2
@IBOutlet weak var updateLocationConfigBtn: UIButton!
3
@IBOutlet weak var updateNotificationConfigBtn: UIButton!
4
@IBOutlet weak var updateDataTrackingConfigBtn: UIButton!
5
@IBOutlet weak var configurationInfo: UILabel!
6
7
override func viewDidLoad() {
8
super.viewDidLoad()
9
let assetTracking = AssetTracking.shared
10
11
let locationConfig = LocationConfig(trackingMode: .ACTIVE)
12
locationConfig.distanceFilter = 5
13
assetTracking.setLocationConfig(config: locationConfig)
14
15
let notificationConfig = NotificationConfig()
16
notificationConfig.showLowBatteryNotification = true
17
assetTracking.setNotificationConfig(config: notificationConfig)
18
19
let dataTrackingConfig = DataTrackingConfig()
20
dataTrackingConfig.shouldClearLocalDataWhenCollision = false
21
assetTracking.setDataTrackingConfig(config: dataTrackingConfig)
22
23
assetTracking.initialize(apiKey: "YOUR_API_KEY")
24
25
createAsset()
26
initView()
27
}
28
29
override func viewWillAppear(_ animated: Bool) {
30
super.viewWillAppear(animated)
31
self.navigationController?.setNavigationBarHidden(false, animated: true)
32
}
33
34
override func viewWillDisappear(_ animated: Bool) {
35
super.viewWillDisappear(animated)
36
self.navigationController?.setNavigationBarHidden(true, animated: true)
37
}
38
39
func initView(){
40
updateLocationConfigBtn.addTarget(self, action: #selector(updateLocationConfig), for: .touchUpInside)
41
updateNotificationConfigBtn.addTarget(self, action: #selector(updateNotificationConfig), for: .touchUpInside)
42
updateDataTrackingConfigBtn.addTarget(self, action: #selector(updateDataTrackingConfig), for: .touchUpInside)
43
configurationInfo.text = ""
44
}
45
46
@objc func updateLocationConfig(){
47
let locationConfig = AssetTracking.shared.getLocationConfig()
48
locationConfig.distanceFilter = 10
49
50
AssetTracking.shared.updateLocationConfig(config: locationConfig)
51
52
let newLocationConfig = AssetTracking.shared.getLocationConfig()
53
54
configurationInfo.text = "The updated location config value is: " + String(newLocationConfig.distanceFilter)
55
}
56
57
@objc func updateNotificationConfig() {
58
let notificationConfig = AssetTracking.shared.getNotificationConfig()
59
notificationConfig.showLowBatteryNotification = true
60
61
AssetTracking.shared.updateNotificationConfig(config: notificationConfig)
62
63
let newNotificationConfig = AssetTracking.shared.getNotificationConfig()
64
65
configurationInfo.text = "The updated notificationConfig config value is: " + String(newNotificationConfig.showLowBatteryNotification)
66
}
67
68
@objc func updateDataTrackingConfig() {
69
let dataTrackingConfig = AssetTracking.shared.getDataTrackingConfig()
70
dataTrackingConfig.shouldClearLocalDataWhenCollision = true
71
72
AssetTracking.shared.updateDataTrackingConfig(config: dataTrackingConfig)
73
74
let newDataTrackingConfig = AssetTracking.shared.getDataTrackingConfig()
75
76
configurationInfo.text = "The updated dataTrackingConfig config value is: " + String(newDataTrackingConfig.shouldClearLocalDataWhenCollision)
77
}
78
79
80
func createAsset(){
81
let attributes = ["attribute 1": "test 1", "attribute 2": "test 2"]
82
let assetProfile: AssetProfile = AssetProfile.init(customId: UUID().uuidString.lowercased(), assetDescription: "testDescription", name: "testName", attributes: attributes)
83
84
AssetTracking.shared.createAsset(assetProfile: assetProfile) { assetCreationResponse in
85
let assetId = assetCreationResponse.data.id
86
87
let toastView = ToastView(message: "Create asset successfully with id: " + assetId)
88
toastView.show()
89
90
self.bindAssetAndStartTracking(assetId: assetId)
91
} errorHandler: { error in
92
let errorMessage = error.localizedDescription
93
let toastView = ToastView(message: "Create asset failed: " + errorMessage)
94
toastView.show()
95
}
96
}
97
98
func bindAssetAndStartTracking(assetId: String) {
99
AssetTracking.shared.bindAsset(assetId: assetId) { responseCode in
100
let toastView = ToastView(message: "Bind asset successfully with id: " + assetId)
101
toastView.show()
102
AssetTracking.shared.startTracking()
103
} errorHandler: { error in
104
let errorMessage = error.localizedDescription
105
let toastView = ToastView(message: "Bind asset failed: " + errorMessage)
106
toastView.show()
107
}
108
}
109
110
}

Upon executing the code example provided above, your app's appearance will resemble the following snippet:

documentation image

Code Highlights

The above code snippet is a class inherited from the UIViewController class, which is the base class for all view controllers in iOS.

  • The class has three outlet properties: updateLocationConfigBtn, updateNotificationConfigBtn, and updateDataTrackingConfigBtn. These properties are linked to the corresponding UI elements in the view controller's storyboard.

  • The class has three methods to update the location configuration, notification configuration, and data tracking configuration, respectively. These methods call the AssetTracking.shared.updateLocationConfig(), AssetTracking.shared.updateNotificationConfig(), and AssetTracking.shared.updateDataTrackingConfig() methods, respectively.

  • The class has a createAsset() method to create a new asset.

  • The class has a bindAssetAndStartTracking() method to bind the asset to the current view controller and start tracking it.

Here is a more detailed explanation of some of the key concepts in the code:

  • AssetTracking: This is the class that is responsible for tracking assets.

  • LocationConfig: This class represents the configuration for location tracking.

  • NotificationConfig: This class represents the configuration for notification settings.

  • DataTrackingConfig: This class represents the configuration for data tracking settings.

© 2024 NextBillion.ai all rights reserved.