Get AssetTrackingCallback ViewController

This example will provide step-by-step guidance on:

  • Initializing Configurations with Default Values: Learn how to set up all configurations using default values, simplifying the integration process and ensuring a seamless start.

  • Creating and Binding a Simple Asset while Initiating Tracking: Discover how to create a basic asset, bind it to your application, and kickstart the tracking process, enabling real-time monitoring of asset locations.

  • Receiving AssetTrackingCallback in Your ViewController: Understand how to receive and utilize AssetTrackingCallback in your ViewController, enabling you to respond to tracking events and location updates effectively.

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

GetAssetCallbackViewController view source

1
class GetAssetCallbackViewController: UIViewController, AssetTrackingCallback {
2
@IBOutlet weak var startTrackingBtn: UIButton!
3
@IBOutlet weak var stopTrackingBtn: UIButton!
4
@IBOutlet weak var trackingStatus: UILabel!
5
@IBOutlet weak var locationInfo: UILabel!
6
7
override func viewDidLoad() {
8
super.viewDidLoad()
9
AssetTracking.shared.initialize(apiKey: "YOUR_API_KEY")
10
11
// Add this to confirm the protocol and receive callbacks
12
AssetTracking.shared.delegate = self
13
14
createAndBindAsset()
15
initView()
16
}
17
18
override func viewWillAppear(_ animated: Bool) {
19
super.viewWillAppear(animated)
20
self.navigationController?.setNavigationBarHidden(false, animated: true)
21
}
22
23
override func viewWillDisappear(_ animated: Bool) {
24
super.viewWillDisappear(animated)
25
self.navigationController?.setNavigationBarHidden(true, animated: true)
26
}
27
28
override func viewDidDisappear(_ animated: Bool) {
29
super.viewDidDisappear(animated)
30
AssetTracking.shared.stopTracking()
31
}
32
33
func initView(){
34
startTrackingBtn.addTarget(self, action: #selector(startTracking), for: .touchUpInside)
35
stopTrackingBtn.addTarget(self, action: #selector(stopTracking), for: .touchUpInside)
36
trackingStatus.text = ""
37
locationInfo.text = ""
38
}
39
40
func createAndBindAsset(){
41
let attributes = ["attribute 1": "test 1", "attribute 2": "test 2"]
42
let assetProfile: AssetProfile = AssetProfile.init(customId: UUID().uuidString.lowercased(), assetDescription: "testDescription", name: "testName", attributes: attributes)
43
44
AssetTracking.shared.createAsset(assetProfile: assetProfile) { assetCreationResponse in
45
let assetId = assetCreationResponse.data.id
46
47
let toastView = ToastView(message: "Create asset successfully with id: " + assetId)
48
toastView.show()
49
50
self.bindAsset(assetId: assetId)
51
} errorHandler: { error in
52
let errorMessage = error.localizedDescription
53
let toastView = ToastView(message: "Create asset failed: " + errorMessage)
54
toastView.show()
55
}
56
}
57
58
func bindAsset(assetId: String) {
59
AssetTracking.shared.bindAsset(assetId: assetId) { responseCode in
60
let toastView = ToastView(message: "Bind asset successfully with id: " + assetId)
61
toastView.show()
62
} errorHandler: { error in
63
let errorMessage = error.localizedDescription
64
let toastView = ToastView(message: "Bind asset failed: " + errorMessage)
65
toastView.show()
66
}
67
}
68
69
@objc func startTracking() {
70
AssetTracking.shared.startTracking()
71
}
72
73
@objc func stopTracking() {
74
AssetTracking.shared.stopTracking()
75
}
76
77
func onTrackingStart(assetId: String) {
78
updateTrackingStatus()
79
}
80
81
func onTrackingStop(assetId: String, trackingDisableType: NBAssetTracking.TrackingDisableType) {
82
updateTrackingStatus()
83
}
84
85
func onLocationSuccess(location: CLLocation) {
86
locationInfo.text = """
87
--------- Location Info ---------
88
Latitude: \(location.coordinate.latitude)
89
Longitude: \(location.coordinate.longitude)
90
Altitude: \(location.altitude)
91
Accuracy: \(location.horizontalAccuracy)
92
Speed: \(location.speed)
93
Bearing: \(location.course)
94
Time: \(location.timestamp)
95
"""
96
}
97
98
func onLocationFailure(error: Error) {
99
locationInfo.text = "Failed to get location data: " + error.localizedDescription
100
}
101
102
func onLocationServiceOff() {
103
showLocationAlert()
104
}
105
106
func showLocationAlert() {
107
let alert = UIAlertController(title: "Location Services Disabled", message: "To enable location services, please go to Settings > Privacy > Location Services.", preferredStyle: .alert)
108
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
109
110
present(alert, animated: true, completion: nil)
111
}
112
113
func updateTrackingStatus() {
114
let assetTrackingRunning = AssetTracking.shared.isRunning()
115
trackingStatus.text = "Tracking Status: \(assetTrackingRunning ? "ON" : "OFF")"
116
if !assetTrackingRunning {
117
locationInfo.text = ""
118
}
119
}
120
}

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 also conforms to the AssetTrackingCallback protocol, which means that the class will receive callbacks from the AssetTracking class.

  • The class has four outlet properties: startTrackingBtn, stopTrackingBtn, trackingStatus, and locationInfo. These properties are linked to the corresponding UI elements in the view controller's storyboard.

  • The class has two methods to start and stop tracking the asset. These methods call the AssetTracking.shared.startTracking() and AssetTracking.shared.stopTracking() methods, respectively.

  • The class has six methods to handle the callbacks from the AssetTracking class. These methods are:

    • onTrackingStart(assetId: String): This method is called when the tracking for the asset starts.

    • onTrackingStop(assetId: String, trackingDisableType: NBAssetTracking.TrackingDisableType): This method is called when the tracking for the asset stops.

    • onLocationSuccess(location: CLLocation): This method is called when the location of the asset is successfully retrieved.

    • onLocationFailure(error: Error): This method is called when the location of the asset cannot be retrieved.

    • onLocationServiceOff(): This method is called when the location services are turned off.

    • showLocationAlert(): This method shows an alert to the user to turn on the location services.

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.

  • AssetTrackingCallback: This is a protocol that must be adopted by classes that want to receive callbacks from the AssetTracking class.

  • CLLocation: This is a class that represents a location on Earth.

© 2024 NextBillion.ai all rights reserved.