iOS Examples

< Back

This document contains practical examples of using the Locator SDK for iOS.

To view the complete Swift templates, see Templates.

1. Basic Initialization

To start using the SDK, you need to initialize it. We recommend that you do this in the application(_:didFinishLaunchingWithOptions:) method of the AppDelegate. If you are not using the AppDelegate file, initialize the SDK in the class that starts your application.

import AppLocatorSDK

class AppDelegate: UIResponder, UIApplicationDelegate {
  func  application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    Task {
      do {
        try await LocatorServiceSdk.shared.start()
      } catch {
        if let sdkError = error as? LocatorServiceSdkConfigurationError {
          presenter.openAlert(text: sdkError.message)
          return
        }
        
        if let sdkError = error as? LocatorServiceSdkPermissionError {
          if sdkError.permissions.contains(.LOCATION) || sdkError.permissions.contains(.BACKGROUND_LOCATION) {
            presenter.openAlert(text: "Falta permissão de localização")
            return
          }
          
          if sdkError.permissions.contains(.MICROPHONE_ACESS) {
            presenter.openAlert(text: "Falta permissão de áudio")
            return
          }
          
          if sdkError.permissions.contains(.USER_NOTIFICATIONS) {
            presenter.openAlert(text: "Falta permissão de notificações")
            return
          }
        }
      }
    }
    
    return true
  }
}

2. Initialization with configuration

You can initialize the SDK with a pre-configured setup.

3. Initialization with integrator

You can initialize the SDK with a custom integrator. To do this, you need to create a class that implements the LocatorIntegration protocol and pass that class to the registerIntegration(integration: any LocatorIntegration) method.

4. Examples of functions

The SDK has several functions available for configuring and retrieving data. Below we will list and explain each of them, with simple examples using the MyClass class.

Function destroy

Used to delete collected records that are persisted on the device and reset the SDK and stop SDK.

Example usage:

Function execute

Used to execute a specific command.

Example usage:

Function getConfig

Used to retrieve the current SDK configuration.

Example usage:

Function getFeatures

Used to retrieve the list of available SDK features.

Example usage:

Function getGroups

Used to retrieve the groups configured in the SDK.

Example usage:

Function getJwtToken

Used to retrieve the JWT token used for communication via WebSocket (WSS).

Example usage:

Function getSdkMode

Used to retrieve the current SDK operating mode.

Example usage:

Function getSession

Used to retrieve information about the current SDK session.

Example usage:

Function getState

Used to retrieve the current SDK state.

Example usage:

Function getVersion

Used to retrieve the current SDK version in use.

Example usage:

Function pendingPermissions

Used to retrieve the list of permissions that still need to be granted by the user.

Example usage:

Function registerIntegration

Used to register or replace the integration used by the SDK.

Example usage:

Function setConfig

Used to save and apply a new SDK configuration.

Example usage:

Function setFeatures

Used to define or update the available SDK features.

Example usage:

Function setGeofences

Used to configure the geofences that will be monitored by the SDK.

Example usage:

Function setGroups

Used to save the groups that will be used by the SDK and update the groups in MQTT.

Example usage:

Function setMutableLicense

Used to define or update the license used by the SDK.

Example usage:

Function setSdkMode

Used to start the SDK in a specific mode.

Example usage:

Function setState

Used to change the internal SDK state.

Example usage:

Function sendEvents

Used to send a package of custom events to the backend.

Example usage:

Function sendLocations (sem parâmetros)

Used to send collected locations that are stored locally.

Example usage:

Function sendLocations (com parâmetro)

Used to send a specific package of collected location data to the backend.

Example usage:

Function start

Used to fully start the SDK, performing the entire required initialization flow.

Example usage:

Function stop

Used to stop the SDK.

Example usage:

Function syncAll

Used to synchronize all relevant SDK data with the backend.

Example usage:

Function syncConfig

Used to synchronize only the SDK configuration with the backend.

Example usage:

Function syncFeatures

Used to synchronize the available features with the backend.

Example usage:

Function syncGeofences

Used to synchronize only the geofences with the backend.

Example usage:

Function syncGroups

Used to synchronize groups with the backend and update the groups used by MQTT.

Example usage:

Function syncScopes

Used to synchronize additional scopes with the backend.

Example usage:

Last updated