• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

Asset profile Operations

This detailed example walks you through the following steps:

  • Creating a New Asset Based on User Input: Learn how to generate a new asset within your application, leveraging user input to create assets dynamically.

  • Binding User-Created Asset IDs to the Current Device: Discover the process of associating user-generated asset IDs with the device you're currently using, facilitating tracking and management of these assets.

  • Starting and Stopping Tracking Based on User Operations: Gain insights into how to initiate and halt tracking operations, giving users control over the tracking process.

  • Switching Between Tracking Modes Based on User Operations: Learn how to transition between different tracking modes, providing flexibility and adaptability to user preferences.

  • Receiving AssetTrackingCallbacks and Displaying Results in the User Interface: Explore how to handle AssetTrackingCallbacks and effectively communicate tracking results to the user interface, ensuring a seamless and informative user experience.

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

SetProfileViewController view source

1
2class SetProfileViewController: UIViewController, UIGestureRecognizerDelegate {
3 @IBOutlet weak var editCustomId: UITextField!
4 @IBOutlet weak var editAssetName: UITextField!
5 @IBOutlet weak var editAssetDescription: UITextField!
6 @IBOutlet weak var editAssetAttributes: UITextField!
7 @IBOutlet weak var lastAssetId: UITextField!
8 @IBOutlet weak var createAsset: UIButton!
9
10 @IBOutlet weak var editAssetId: UITextField!
11 @IBOutlet weak var bindAsset: UIButton!
12
13 var customId: String = ""
14 var assetName: String = ""
15 var assetDescription: String = ""
16 var assetAttributes: String = ""
17 var assetId: String = ""
18

ExtendedTrackingViewController view source

1class ExtendedTrackingViewController: UIViewController, AssetTrackingCallback {
2 @IBOutlet weak var locationInfo: UILabel!
3 @IBOutlet weak var trackingStatus: UILabel!
4 @IBOutlet weak var trackingModeSelector: UISegmentedControl!
5 @IBOutlet weak var startTracking: UIButton!
6 @IBOutlet weak var stopTracking: UIButton!
7 @IBOutlet weak var createAsset: UIButton!
8 @IBOutlet weak var viewDataLogs: UIButton!
9 var assetTracking: AssetTracking = AssetTracking.shared
10
11 override func viewDidLoad() {
12 super.viewDidLoad()
13 assetTracking.initialize(apiKey: "YOUR_API_KEY")
14
15 assetTracking.delegate = self
16
17 startTracking.setTitle("Start Tracking", for: .normal)
18 startTracking.setTitleColor(.white, for: .normal)

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

docs-imagedocs-image
Asset Profile PageAsset Tracking Page

Code Highlights

The SetProfileViewController class inherits from the UIViewController class, which is the base class for all view controllers in iOS.

  • The class has several outlet properties: editCustomId, editAssetName, editAssetDescription, editAssetAttributes, lastAssetId, createAsset, and bindAsset. These properties are linked to the corresponding UI elements in the view controller's storyboard.

  • createAsset(): This method creates a new asset with the specified custom ID, asset name, description, and attributes. The method calls the AssetTracking.shared.createAsset() method to make the API request.

  • bindAsset(): This method binds the current view controller to the specified asset ID. The method calls the AssetTracking.shared.bindAsset() method to make the API request.

  • saveAssetProfile(): This method saves the asset profile to the user's device. The method updates the values of the customId, assetName, assetDescription, assetAttributes, and assetId properties, and then calls the userDefaults.set() method to save the values to the user's device.

  • onCreateAssetTapped(): This method is called when the user taps on the create asset button. The method checks if the asset name is empty, and if it is, it displays a toast message. Otherwise, it creates a new asset and saves the asset profile.

  • onBindAssetTapped(): This method is called when the user taps on the bind asset button. The method checks if the asset ID is empty, and if it is, it displays a toast message. Otherwise, it binds the current view controller to the asset and saves the asset profile.

  • dismissKeyboard(): This method dismisses the keyboard. The method calls the view.endEditing(true) method to dismiss the keyboard.

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

  • UITextFieldDelegate: This protocol is adopted by classes that want to receive notifications when a text field changes.

  • UIGestureRecognizerDelegate: This protocol is adopted by classes that want to receive notifications when a gesture recognizer is triggered.

  • AssetTrackingApiExceptionType: This enum defines the possible types of errors that can occur when calling the Asset Tracking API.

The ViewController class inherits from the UIViewController class, which is the base class for all view controllers in iOS.

  • The class has several outlet properties: locationInfo, trackingStatus, trackingModeSelector, startTracking, stopTracking, createAsset, and vie**wDataLogs. These properties are linked to the corresponding UI elements in the view controller's storyboard.

  • startTracking(): This method starts tracking the asset. The method calls the assetTracking.startTracking() method.

  • stopTracking(): This method stops tracking the asset. The method calls the assetTracking.stopTracking() method.

  • onTrackingStart(): This method is called when tracking starts. The method updates the trackingStatus label.

  • onTrackingStop(): This method is called when tracking stops. The method updates the trackingStatus label.

  • onLocationSuccess(): This method is called when a new location is received. The method updates the locationInfo label.

  • onLocationFailure(): This method is called when an error occurs while getting the location. The method does nothing in this case.

  • onLocationServiceOff(): This method is called when location services are turned off. The method displays an alert to the user.

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

  • AssetTracking: This class is responsible for tracking assets.

  • TrackingMode: This enum defines the three possible tracking modes: ACTIVE, BALANCED, and PASSIVE.

  • TrackingDisableType: This enum defines the possible reasons why tracking was stopped.

  • CLLocation: This class represents a location on Earth.

  • UIAlertController: This class is used to display alerts.

Have Questions ?