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]
61 // [swift_NavigineSdk_setServer]
62 // Set server URL (optional)
63 sdk.setServer("https://custom.navigine.com")
64 // [swift_NavigineSdk_setServer]
66 // Initialize managers
70 print("SDK initialization error: \(error)")
75 * Initialize all SDK managers
77 private func initializeManagers() {
78 guard let sdk = sdk else { return }
80 // [swift_NavigineSdk_getLocationManager]
81 // Get LocationManager for working with locations
82 locationManager = sdk.getLocationManager()
83 if locationManager != nil {
84 print("LocationManager successfully initialized")
86 // [swift_NavigineSdk_getLocationManager]
88 // [swift_NavigineSdk_getNavigationManager]
89 // Get NavigationManager for navigation
90 if let locationManager = locationManager {
91 navigationManager = sdk.getNavigationManager(locationManager)
92 if navigationManager != nil {
93 print("NavigationManager successfully initialized")
96 // [swift_NavigineSdk_getNavigationManager]
98 // [swift_NavigineSdk_getZoneManager]
99 // Get ZoneManager for working with zones
100 if let navigationManager = navigationManager {
101 zoneManager = sdk.getZoneManager(navigationManager)
102 if zoneManager != nil {
103 print("ZoneManager successfully initialized")
106 // [swift_NavigineSdk_getZoneManager]
108 // [swift_NavigineSdk_getRouteManager]
109 // Get RouteManager for building routes
110 if let locationManager = locationManager,
111 let navigationManager = navigationManager {
112 routeManager = sdk.getRouteManager(locationManager, navigationManager: navigationManager)
113 if routeManager != nil {
114 print("RouteManager successfully initialized")
117 // [swift_NavigineSdk_getRouteManager]
119 // [swift_NavigineSdk_getAsyncRouteManager]
120 // Get AsyncRouteManager for async route operations
121 if let locationManager = locationManager,
122 let navigationManager = navigationManager {
123 asyncRouteManager = sdk.getAsyncRouteManager(locationManager, navigationManager: navigationManager)
124 if asyncRouteManager != nil {
125 print("AsyncRouteManager successfully initialized")
128 // [swift_NavigineSdk_getAsyncRouteManager]
130 // [swift_NavigineSdk_getNotificationManager]
131 // Get NotificationManager for notifications
132 if let locationManager = locationManager {
133 notificationManager = sdk.getNotificationManager(locationManager)
134 if notificationManager != nil {
135 print("NotificationManager successfully initialized")
138 // [swift_NavigineSdk_getNotificationManager]
140 // [swift_NavigineSdk_getMeasurementManager]
141 // Get MeasurementManager for measurements
142 if let locationManager = locationManager {
143 measurementManager = sdk.getMeasurementManager(locationManager)
144 if measurementManager != nil {
145 print("MeasurementManager successfully initialized")
148 // [swift_NavigineSdk_getMeasurementManager]
150 // [swift_NavigineSdk_getLocationListManager]
151 // Get LocationListManager for location list operations
152 locationListManager = sdk.getLocationListManager()
153 if locationListManager != nil {
154 print("LocationListManager successfully initialized")
156 // [swift_NavigineSdk_getLocationListManager]
158 // [swift_NavigineSdk_getStorageManager]
159 // Get StorageManager for working with storages
160 storageManager = sdk.getStorageManager()
161 if storageManager != nil {
162 print("StorageManager successfully initialized")
164 // [swift_NavigineSdk_getStorageManager]
166 // [swift_NavigineSdk_getMqttSession]
167 // Get MqttSession for publishing position data via MQTT
168 if let navigationManager = navigationManager {
169 mqttSession = sdk.getMqttSession(navigationManager)
170 if mqttSession != nil {
171 print("MqttSession successfully initialized")
174 // [swift_NavigineSdk_getMqttSession]
178 * Get information about current SDK state
180 func printSdkInfo() {
181 print("=== Navigine SDK Information ===")
182 print("SDK Version: \(NCNavigineSdk.getVersion())")
183 print("Device ID: \(NCNavigineSdk.getDeviceId())")
184 print("Relative Time: \(NCNavigineSdk.getRelativeTime())")
185 print("LocationManager: \(locationManager != nil ? "available" : "unavailable")")
186 print("NavigationManager: \(navigationManager != nil ? "available" : "unavailable")")
187 print("ZoneManager: \(zoneManager != nil ? "available" : "unavailable")")
188 print("RouteManager: \(routeManager != nil ? "available" : "unavailable")")
189 print("AsyncRouteManager: \(asyncRouteManager != nil ? "available" : "unavailable")")
190 print("NotificationManager: \(notificationManager != nil ? "available" : "unavailable")")
191 print("MeasurementManager: \(measurementManager != nil ? "available" : "unavailable")")
192 print("LocationListManager: \(locationListManager != nil ? "available" : "unavailable")")
193 print("StorageManager: \(storageManager != nil ? "available" : "unavailable")")
194 print("MqttSession: \(mqttSession != nil ? "available" : "unavailable")")
198 * Demonstrate usage of all managers
200 func demonstrateManagersUsage() {
201 print("=== Manager Usage Demonstration ===")
203 // LocationManager usage example
204 if let locationManager = locationManager {
205 print("LocationManager ready for location operations")
206 // Add code here for loading locations, working with them, etc.
209 // NavigationManager usage example
210 if let navigationManager = navigationManager {
211 print("NavigationManager ready for positioning")
212 // Add code here for getting position, setting up listeners, etc.
215 // ZoneManager usage example
216 if let zoneManager = zoneManager {
217 print("ZoneManager ready for zone operations")
218 // Add code here for setting up zones, handling enter/exit events, etc.
221 // RouteManager usage example
222 if let routeManager = routeManager {
223 print("RouteManager ready for route building")
224 // Add code here for building routes, setting destination points, etc.
227 // AsyncRouteManager usage example
228 if let asyncRouteManager = asyncRouteManager {
229 print("AsyncRouteManager ready for async route operations")
230 // Add code here for creating routing sessions, etc.
233 // NotificationManager usage example
234 if let notificationManager = notificationManager {
235 print("NotificationManager ready for notifications")
236 // Add code here for setting up beacon notifications, etc.
239 // MeasurementManager usage example
240 if let measurementManager = measurementManager {
241 print("MeasurementManager ready for measurements")
242 // Add code here for managing measurement generators, etc.
245 // LocationListManager usage example
246 if let locationListManager = locationListManager {
247 print("LocationListManager ready for location list operations")
248 // Add code here for getting available locations list, etc.
251 // StorageManager usage example
252 if let storageManager = storageManager {
253 print("StorageManager ready for storage operations")
254 // Add code here for creating storages, managing data, etc.
257 // MqttSession usage example
258 if let mqttSession = mqttSession {
259 print("MqttSession ready for publishing position data via MQTT")
260 // Add code here for connecting to MQTT broker, publishing positions, etc.
265 * Demonstrate Swift features usage
267 func demonstrateSwiftFeatures() {
268 print("=== Swift Features Demonstration ===")
270 // Using guard let for safe optional unwrapping
271 guard let sdk = sdk else {
272 print("SDK unavailable")
276 // Using defer for cleanup code execution
278 print("Swift features demonstration completed")
281 // Using map for data transformation
283 "version": NCNavigineSdk.getVersion(),
284 "deviceId": NCNavigineSdk.getDeviceId(),
285 "relativeTime": NCNavigineSdk.getRelativeTime()
288 let infoStrings = sdkInfo.map { key, value in
291 print("SDK Information: \(infoStrings.joined(separator: ", "))")
293 // Using compactMap for nil filtering
307 let availableManagers = managers.compactMap { $0 }
308 print("Available managers: \(availableManagers.count)")
310 // Using async/await (if supported)
312 await performAsyncOperation()
320 private func performAsyncOperation() async {
321 // Simulate async operation
322 try? await Task.sleep(nanoseconds: 1_000_000_000) // 1 second
323 print("Async operation completed")
327 * Demonstrate property wrappers usage
329 func demonstratePropertyWrappers() {
330 print("=== Property Wrappers Demonstration ===")
332 // Example of @Published usage (if this was an ObservableObject)
333 // @Published var sdkVersion = NCNavigineSdk.getVersion()
335 // Example of @State usage (for SwiftUI)
336 // @State private var isInitialized = false
338 print("Property wrappers demonstrated")
342 * Main method for demonstration
346 demonstrateManagersUsage()
347 demonstrateSwiftFeatures()
348 demonstratePropertyWrappers()
353 * Extension for additional functionality
355extension NavigineSdkExample {
358 * Get status of all managers
360 func getManagersStatus() -> [String: Bool] {
362 "LocationManager": locationManager != nil,
363 "NavigationManager": navigationManager != nil,
364 "ZoneManager": zoneManager != nil,
365 "RouteManager": routeManager != nil,
366 "AsyncRouteManager": asyncRouteManager != nil,
367 "NotificationManager": notificationManager != nil,
368 "MeasurementManager": measurementManager != nil,
369 "LocationListManager": locationListManager != nil,
370 "StorageManager": storageManager != nil,
371 "MqttSession": mqttSession != nil
376 * Check if SDK is ready for work
378 func isSdkReady() -> Bool {
379 return sdk != nil && locationManager != nil
384 * Function to run the example
387 let example = NavigineSdkExample()