5 * NavigineSDK usage example for Swift
6 * Demonstrates SDK initialization and access to various managers
8class NavigineSdkExample {
10 private var sdk: NCNavigineSdk?
11 private var locationManager: NCLocationManager?
12 private var navigationManager: NCNavigationManager?
13 private var zoneManager: NCZoneManager?
14 private var routeManager: NCRouteManager?
15 private var asyncRouteManager: NCAsyncRouteManager?
16 private var notificationManager: NCNotificationManager?
17 private var measurementManager: NCMeasurementManager?
18 private var locationListManager: NCLocationListManager?
19 private var storageManager: NCStorageManager?
20 private var mqttSession: NCMqttSession?
27 * SDK initialization and basic parameter setup
29 private func initializeSdk() {
31 // [swift_NavigineSdk_getInstance]
33 sdk = NCNavigineSdk.getInstance()
34 // [swift_NavigineSdk_getInstance]
36 guard let sdk = sdk else {
37 print("Error: failed to get NavigineSdk instance")
41 // [swift_NavigineSdk_getVersion]
43 print("Navigine SDK Version: \(NCNavigineSdk.getVersion())")
44 // [swift_NavigineSdk_getVersion]
46 // [swift_NavigineSdk_getDeviceId]
48 print("Device ID: \(NCNavigineSdk.getDeviceId())")
49 // [swift_NavigineSdk_getDeviceId]
51 // [swift_NavigineSdk_getRelativeTime]
53 print("Relative Time: \(NCNavigineSdk.getRelativeTime())")
54 // [swift_NavigineSdk_getRelativeTime]
56 // [swift_NavigineSdk_setUserHash]
57 // Set user hash (authorization token)
58 sdk.setUserHash("XXXX-XXXX-XXXX-XXXX")
59 // [swift_NavigineSdk_setUserHash]
62 // [swift_NavigineSdk_getUserAgent]
63 print("User-Agent: \(NCNavigineSdk.getUserAgent())")
64 // [swift_NavigineSdk_getUserAgent]
66 // [swift_NavigineSdk_reset]
68 print("SDK reset to initial state")
69 // [swift_NavigineSdk_reset]
71 // [swift_NavigineSdk_setServer]
72 // Set server URL (optional)
73 sdk.setServer("https://custom.navigine.com")
74 // [swift_NavigineSdk_setServer]
76 // Initialize managers
80 print("SDK initialization error: \(error)")
85 * Initialize all SDK managers
87 private func initializeManagers() {
88 guard let sdk = sdk else { return }
90 // [swift_NavigineSdk_getLocationManager]
91 // Get LocationManager for working with locations
92 locationManager = sdk.getLocationManager()
93 if locationManager != nil {
94 print("LocationManager successfully initialized")
96 // [swift_NavigineSdk_getLocationManager]
98 // [swift_NavigineSdk_getNavigationManager]
99 // Get NavigationManager for navigation
100 if let locationManager = locationManager {
101 navigationManager = sdk.getNavigationManager(locationManager)
102 if navigationManager != nil {
103 print("NavigationManager successfully initialized")
106 // [swift_NavigineSdk_getNavigationManager]
108 // [swift_NavigineSdk_getZoneManager]
109 // Get ZoneManager for working with zones
110 if let navigationManager = navigationManager {
111 zoneManager = sdk.getZoneManager(navigationManager)
112 if zoneManager != nil {
113 print("ZoneManager successfully initialized")
116 // [swift_NavigineSdk_getZoneManager]
118 // [swift_NavigineSdk_getRouteManager]
119 // Get RouteManager for building routes
120 if let locationManager = locationManager,
121 let navigationManager = navigationManager {
122 routeManager = sdk.getRouteManager(locationManager, navigationManager: navigationManager)
123 if routeManager != nil {
124 print("RouteManager successfully initialized")
127 // [swift_NavigineSdk_getRouteManager]
129 // [swift_NavigineSdk_getAsyncRouteManager]
130 // Get AsyncRouteManager for async route operations
131 if let locationManager = locationManager,
132 let navigationManager = navigationManager {
133 asyncRouteManager = sdk.getAsyncRouteManager(locationManager, navigationManager: navigationManager)
134 if asyncRouteManager != nil {
135 print("AsyncRouteManager successfully initialized")
138 // [swift_NavigineSdk_getAsyncRouteManager]
140 // [swift_NavigineSdk_getNotificationManager]
141 // Get NotificationManager for notifications
142 if let locationManager = locationManager {
143 notificationManager = sdk.getNotificationManager(locationManager)
144 if notificationManager != nil {
145 print("NotificationManager successfully initialized")
148 // [swift_NavigineSdk_getNotificationManager]
150 // [swift_NavigineSdk_getMeasurementManager]
151 // Get MeasurementManager for measurements
152 if let locationManager = locationManager {
153 measurementManager = sdk.getMeasurementManager(locationManager)
154 if measurementManager != nil {
155 print("MeasurementManager successfully initialized")
158 // [swift_NavigineSdk_getMeasurementManager]
160 // [swift_NavigineSdk_getLocationListManager]
161 // Get LocationListManager for location list operations
162 locationListManager = sdk.getLocationListManager()
163 if locationListManager != nil {
164 print("LocationListManager successfully initialized")
166 // [swift_NavigineSdk_getLocationListManager]
168 // [swift_NavigineSdk_getStorageManager]
169 // Get StorageManager for working with storages
170 storageManager = sdk.getStorageManager()
171 if storageManager != nil {
172 print("StorageManager successfully initialized")
174 // [swift_NavigineSdk_getStorageManager]
176 // [swift_NavigineSdk_getMqttSession]
177 // Get MqttSession for publishing position data via MQTT
178 if let navigationManager = navigationManager {
179 mqttSession = sdk.getMqttSession(navigationManager)
180 if mqttSession != nil {
181 print("MqttSession successfully initialized")
184 // [swift_NavigineSdk_getMqttSession]
188 * Get information about current SDK state
190 func printSdkInfo() {
191 print("=== Navigine SDK Information ===")
192 print("SDK Version: \(NCNavigineSdk.getVersion())")
193 print("Device ID: \(NCNavigineSdk.getDeviceId())")
194 print("Relative Time: \(NCNavigineSdk.getRelativeTime())")
195 print("LocationManager: \(locationManager != nil ? "available" : "unavailable")")
196 print("NavigationManager: \(navigationManager != nil ? "available" : "unavailable")")
197 print("ZoneManager: \(zoneManager != nil ? "available" : "unavailable")")
198 print("RouteManager: \(routeManager != nil ? "available" : "unavailable")")
199 print("AsyncRouteManager: \(asyncRouteManager != nil ? "available" : "unavailable")")
200 print("NotificationManager: \(notificationManager != nil ? "available" : "unavailable")")
201 print("MeasurementManager: \(measurementManager != nil ? "available" : "unavailable")")
202 print("LocationListManager: \(locationListManager != nil ? "available" : "unavailable")")
203 print("StorageManager: \(storageManager != nil ? "available" : "unavailable")")
204 print("MqttSession: \(mqttSession != nil ? "available" : "unavailable")")
208 * Demonstrate usage of all managers
210 func demonstrateManagersUsage() {
211 print("=== Manager Usage Demonstration ===")
213 // LocationManager usage example
214 if let locationManager = locationManager {
215 print("LocationManager ready for location operations")
216 // Add code here for loading locations, working with them, etc.
219 // NavigationManager usage example
220 if let navigationManager = navigationManager {
221 print("NavigationManager ready for positioning")
222 // Add code here for getting position, setting up listeners, etc.
225 // ZoneManager usage example
226 if let zoneManager = zoneManager {
227 print("ZoneManager ready for zone operations")
228 // Add code here for setting up zones, handling enter/exit events, etc.
231 // RouteManager usage example
232 if let routeManager = routeManager {
233 print("RouteManager ready for route building")
234 // Add code here for building routes, setting destination points, etc.
237 // AsyncRouteManager usage example
238 if let asyncRouteManager = asyncRouteManager {
239 print("AsyncRouteManager ready for async route operations")
240 // Add code here for creating routing sessions, etc.
243 // NotificationManager usage example
244 if let notificationManager = notificationManager {
245 print("NotificationManager ready for notifications")
246 // Add code here for setting up beacon notifications, etc.
249 // MeasurementManager usage example
250 if let measurementManager = measurementManager {
251 print("MeasurementManager ready for measurements")
252 // Add code here for managing measurement generators, etc.
255 // LocationListManager usage example
256 if let locationListManager = locationListManager {
257 print("LocationListManager ready for location list operations")
258 // Add code here for getting available locations list, etc.
261 // StorageManager usage example
262 if let storageManager = storageManager {
263 print("StorageManager ready for storage operations")
264 // Add code here for creating storages, managing data, etc.
267 // MqttSession usage example
268 if let mqttSession = mqttSession {
269 print("MqttSession ready for publishing position data via MQTT")
270 // Add code here for connecting to MQTT broker, publishing positions, etc.
275 * Demonstrate Swift features usage
277 func demonstrateSwiftFeatures() {
278 print("=== Swift Features Demonstration ===")
280 // Using guard let for safe optional unwrapping
281 guard let sdk = sdk else {
282 print("SDK unavailable")
286 // Using defer for cleanup code execution
288 print("Swift features demonstration completed")
291 // Using map for data transformation
293 "version": NCNavigineSdk.getVersion(),
294 "deviceId": NCNavigineSdk.getDeviceId(),
295 "relativeTime": NCNavigineSdk.getRelativeTime()
298 let infoStrings = sdkInfo.map { key, value in
301 print("SDK Information: \(infoStrings.joined(separator: ", "))")
303 // Using compactMap for nil filtering
317 let availableManagers = managers.compactMap { $0 }
318 print("Available managers: \(availableManagers.count)")
320 // Using async/await (if supported)
322 await performAsyncOperation()
330 private func performAsyncOperation() async {
331 // Simulate async operation
332 try? await Task.sleep(nanoseconds: 1_000_000_000) // 1 second
333 print("Async operation completed")
337 * Demonstrate property wrappers usage
339 func demonstratePropertyWrappers() {
340 print("=== Property Wrappers Demonstration ===")
342 // Example of @Published usage (if this was an ObservableObject)
343 // @Published var sdkVersion = NCNavigineSdk.getVersion()
345 // Example of @State usage (for SwiftUI)
346 // @State private var isInitialized = false
348 print("Property wrappers demonstrated")
352 * Main method for demonstration
356 demonstrateManagersUsage()
357 demonstrateSwiftFeatures()
358 demonstratePropertyWrappers()
363 * Extension for additional functionality
365extension NavigineSdkExample {
368 * Get status of all managers
370 func getManagersStatus() -> [String: Bool] {
372 "LocationManager": locationManager != nil,
373 "NavigationManager": navigationManager != nil,
374 "ZoneManager": zoneManager != nil,
375 "RouteManager": routeManager != nil,
376 "AsyncRouteManager": asyncRouteManager != nil,
377 "NotificationManager": notificationManager != nil,
378 "MeasurementManager": measurementManager != nil,
379 "LocationListManager": locationListManager != nil,
380 "StorageManager": storageManager != nil,
381 "MqttSession": mqttSession != nil
386 * Check if SDK is ready for work
388 func isSdkReady() -> Bool {
389 return sdk != nil && locationManager != nil
394 * Function to run the example
397 let example = NavigineSdkExample()