Simple Asset Tracking Example

This example provides step by step guidance on:

  1. Initializing Configurations with Default Values: Learn how to set up the SDK with default configuration settings, ensuring a straightforward integration process.

  2. Creating and Binding a Simple Asset: Discover how to create and bind an asset within your application, enabling you to associate assets with your tracking system.

  3. Starting Tracking and Uploading Location Data with Default Values: Gain insights into how to initiate tracking and the automatic uploading of location data using default parameters, simplifying the process of monitoring asset locations.

For all code examples, refer to iOS Tracking Code Examples

SimpleTrackingViewController view source

1
class SimpleTrackingViewController: UIViewController {
2
@IBOutlet weak var startTracking: UIButton!
3
@IBOutlet weak var assetId: UILabel!
4
@IBOutlet weak var trackingStatus: UILabel!
5
6
private var mAssetId: String = ""
7
8
override func viewDidLoad() {
9
super.viewDidLoad()
10
AssetTracking.shared.initialize(apiKey: "YOUR_API_KEY")
11
12
initView()
13
14
createAsset()
15
}
16
17
override func viewWillAppear(_ animated: Bool) {
18
super.viewWillAppear(animated)
19
self.navigationController?.setNavigationBarHidden(false, animated: true)
20
}
21
22
override func viewWillDisappear(_ animated: Bool) {
23
super.viewWillDisappear(animated)
24
self.navigationController?.setNavigationBarHidden(true, animated: true)
25
}
26
27
override func viewDidDisappear(_ animated: Bool) {
28
super.viewDidDisappear(animated)
29
AssetTracking.shared.stopTracking()
30
}
31
32
func initView() {
33
startTracking.addTarget(self, action: #selector(bindAssetAndStartTracking), for: .touchUpInside)
34
assetId.text = ""
35
trackingStatus.text = ""
36
}
37
38
func createAsset(){
39
let attributes = ["attribute 1": "test 1", "attribute 2": "test 2"]
40
let assetProfile: AssetProfile = AssetProfile.init(customId: UUID().uuidString.lowercased(), assetDescription: "testDescription", name: "testName", attributes: attributes)
41
42
AssetTracking.shared.createAsset(assetProfile: assetProfile) { assetCreationResponse in
43
let assetId = assetCreationResponse.data.id
44
45
let toastView = ToastView(message: "Create asset successfully with id: " + assetId)
46
toastView.show()
47
48
self.mAssetId = assetId
49
self.assetId.text = "asset id is: " + assetId
50
} errorHandler: { error in
51
let errorMessage = error.localizedDescription
52
let toastView = ToastView(message: "Create asset failed: " + errorMessage)
53
toastView.show()
54
}
55
}
56
57
@objc func bindAssetAndStartTracking() {
58
AssetTracking.shared.bindAsset(assetId: mAssetId) { responseCode in
59
let toastView = ToastView(message: "Bind asset successfully with id: " + self.mAssetId)
60
toastView.show()
61
AssetTracking.shared.startTracking()
62
self.trackingStatus.text = "Asset Tracking is running"
63
} errorHandler: { error in
64
let errorMessage = error.localizedDescription
65
let toastView = ToastView(message: "Bind asset failed: " + errorMessage)
66
toastView.show()
67
}
68
}
69
}

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: startTracking, assetId, and trackingStatus. These properties are linked to the corresponding UI elements in the view controller's storyboard.

  • The class has a private variable called mAssetId, which stores the ID of the asset that is being tracked.

  • The viewDidLoad() method is called when the view controller is first loaded. In this method, the class initializes the AssetTracking shared instance and calls the createAsset() method to create a new asset.

  • The viewWillAppear() and viewWillDisappear() methods are called when the view controller is about to appear or disappear, respectively. In these methods, the class hides or shows the navigation bar, depending on the view controller's visibility.

  • The viewDidDisappear() method is called when the view controller has disappeared. In this method, the class stops tracking the asset.

  • The initView() method is called to initialize the view controller's user interface. In this method, the class adds a target-action listener to the startTracking button to start tracking the asset when the button is tapped.

  • The createAsset() method creates a new asset with the specified attributes. The method calls the AssetTracking.shared.createAsset() method to make the API request. The method also handles the success and failure cases of the API request.

  • The bindAssetAndStartTracking() method binds the asset to the current view controller and starts tracking it. The method calls the AssetTracking.shared.bindAsset() method to make the API request. The method also handles the success and failure cases of the API request.

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

  • AssetTracking: This is the shared instance of the AssetTracking class, which is responsible for tracking assets.

  • NBAssetTrackingApiFetcher: This is a class that provides methods for making API requests to the Asset Tracking API.

  • ToastView: This is a class that displays a short message on the screen.

© 2024 NextBillion.ai all rights reserved.