Loading...
Searching...
No Matches
NavigineSdkExample.swift
Go to the documentation of this file.
1import Foundation
2import NavigineSDK
3
4/**
5 * NavigineSDK usage example for Swift
6 * Demonstrates SDK initialization and access to various managers
7 */
8class NavigineSdkExample {
9
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?
21
22 init() {
23 initializeSdk()
24 }
25
26 /**
27 * SDK initialization and basic parameter setup
28 */
29 private func initializeSdk() {
30 do {
31 // [swift_NavigineSdk_getInstance]
32 // Get SDK instance
33 sdk = NCNavigineSdk.getInstance()
34 // [swift_NavigineSdk_getInstance]
35
36 guard let sdk = sdk else {
37 print("Error: failed to get NavigineSdk instance")
38 return
39 }
40
41 // [swift_NavigineSdk_getVersion]
42 // Get SDK version
43 print("Navigine SDK Version: \‍(NCNavigineSdk.getVersion())")
44 // [swift_NavigineSdk_getVersion]
45
46 // [swift_NavigineSdk_getDeviceId]
47 // Get device ID
48 print("Device ID: \‍(NCNavigineSdk.getDeviceId())")
49 // [swift_NavigineSdk_getDeviceId]
50
51 // [swift_NavigineSdk_getRelativeTime]
52 // Get relative time
53 print("Relative Time: \‍(NCNavigineSdk.getRelativeTime())")
54 // [swift_NavigineSdk_getRelativeTime]
55
56 // [swift_NavigineSdk_setUserHash]
57 // Set user hash (authorization token)
58 sdk.setUserHash("XXXX-XXXX-XXXX-XXXX")
59 // [swift_NavigineSdk_setUserHash]
60
61
62 // [swift_NavigineSdk_getUserAgent]
63 print("User-Agent: \‍(NCNavigineSdk.getUserAgent())")
64 // [swift_NavigineSdk_getUserAgent]
65
66 // [swift_NavigineSdk_reset]
67 sdk?.reset()
68 print("SDK reset to initial state")
69 // [swift_NavigineSdk_reset]
70
71 // [swift_NavigineSdk_setServer]
72 // Set server URL (optional)
73 sdk.setServer("https://custom.navigine.com")
74 // [swift_NavigineSdk_setServer]
75
76 // Initialize managers
77 initializeManagers()
78
79 } catch {
80 print("SDK initialization error: \‍(error)")
81 }
82 }
83
84 /**
85 * Initialize all SDK managers
86 */
87 private func initializeManagers() {
88 guard let sdk = sdk else { return }
89
90 // [swift_NavigineSdk_getLocationManager]
91 // Get LocationManager for working with locations
92 locationManager = sdk.getLocationManager()
93 if locationManager != nil {
94 print("LocationManager successfully initialized")
95 }
96 // [swift_NavigineSdk_getLocationManager]
97
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")
104 }
105 }
106 // [swift_NavigineSdk_getNavigationManager]
107
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")
114 }
115 }
116 // [swift_NavigineSdk_getZoneManager]
117
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")
125 }
126 }
127 // [swift_NavigineSdk_getRouteManager]
128
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")
136 }
137 }
138 // [swift_NavigineSdk_getAsyncRouteManager]
139
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")
146 }
147 }
148 // [swift_NavigineSdk_getNotificationManager]
149
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")
156 }
157 }
158 // [swift_NavigineSdk_getMeasurementManager]
159
160 // [swift_NavigineSdk_getLocationListManager]
161 // Get LocationListManager for location list operations
162 locationListManager = sdk.getLocationListManager()
163 if locationListManager != nil {
164 print("LocationListManager successfully initialized")
165 }
166 // [swift_NavigineSdk_getLocationListManager]
167
168 // [swift_NavigineSdk_getStorageManager]
169 // Get StorageManager for working with storages
170 storageManager = sdk.getStorageManager()
171 if storageManager != nil {
172 print("StorageManager successfully initialized")
173 }
174 // [swift_NavigineSdk_getStorageManager]
175
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")
182 }
183 }
184 // [swift_NavigineSdk_getMqttSession]
185 }
186
187 /**
188 * Get information about current SDK state
189 */
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")")
205 }
206
207 /**
208 * Demonstrate usage of all managers
209 */
210 func demonstrateManagersUsage() {
211 print("=== Manager Usage Demonstration ===")
212
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.
217 }
218
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.
223 }
224
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.
229 }
230
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.
235 }
236
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.
241 }
242
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.
247 }
248
249 // MeasurementManager usage example
250 if let measurementManager = measurementManager {
251 print("MeasurementManager ready for measurements")
252 // Add code here for managing measurement generators, etc.
253 }
254
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.
259 }
260
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.
265 }
266
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.
271 }
272 }
273
274 /**
275 * Demonstrate Swift features usage
276 */
277 func demonstrateSwiftFeatures() {
278 print("=== Swift Features Demonstration ===")
279
280 // Using guard let for safe optional unwrapping
281 guard let sdk = sdk else {
282 print("SDK unavailable")
283 return
284 }
285
286 // Using defer for cleanup code execution
287 defer {
288 print("Swift features demonstration completed")
289 }
290
291 // Using map for data transformation
292 let sdkInfo = [
293 "version": NCNavigineSdk.getVersion(),
294 "deviceId": NCNavigineSdk.getDeviceId(),
295 "relativeTime": NCNavigineSdk.getRelativeTime()
296 ]
297
298 let infoStrings = sdkInfo.map { key, value in
299 "\‍(key): \‍(value)"
300 }
301 print("SDK Information: \‍(infoStrings.joined(separator: ", "))")
302
303 // Using compactMap for nil filtering
304 let managers = [
305 locationManager,
306 navigationManager,
307 zoneManager,
308 routeManager,
309 asyncRouteManager,
310 notificationManager,
311 measurementManager,
312 locationListManager,
313 storageManager,
314 mqttSession
315 ]
316
317 let availableManagers = managers.compactMap { $0 }
318 print("Available managers: \‍(availableManagers.count)")
319
320 // Using async/await (if supported)
321 Task {
322 await performAsyncOperation()
323 }
324 }
325
326 /**
327 * Async operation
328 */
329 @MainActor
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")
334 }
335
336 /**
337 * Demonstrate property wrappers usage
338 */
339 func demonstratePropertyWrappers() {
340 print("=== Property Wrappers Demonstration ===")
341
342 // Example of @Published usage (if this was an ObservableObject)
343 // @Published var sdkVersion = NCNavigineSdk.getVersion()
344
345 // Example of @State usage (for SwiftUI)
346 // @State private var isInitialized = false
347
348 print("Property wrappers demonstrated")
349 }
350
351 /**
352 * Main method for demonstration
353 */
354 func runExample() {
355 printSdkInfo()
356 demonstrateManagersUsage()
357 demonstrateSwiftFeatures()
358 demonstratePropertyWrappers()
359 }
360}
361
362/**
363 * Extension for additional functionality
364 */
365extension NavigineSdkExample {
366
367 /**
368 * Get status of all managers
369 */
370 func getManagersStatus() -> [String: Bool] {
371 return [
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
382 ]
383 }
384
385 /**
386 * Check if SDK is ready for work
387 */
388 func isSdkReady() -> Bool {
389 return sdk != nil && locationManager != nil
390 }
391}
392
393/**
394 * Function to run the example
395 */
396func main() {
397 let example = NavigineSdkExample()
398 example.runExample()
399}
400
401// Run the example
402main()