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 private var locationWindow: NCLocationWindow?
22 private var routeLayer: NCRouteLayer?
23 private var guidanceCamera: NCGuidanceCamera?
24
25 init() {
26 initializeSdk()
27 }
28
29 /**
30 * SDK initialization and basic parameter setup
31 */
32 private func initializeSdk() {
33 do {
34 // [swift_NavigineSdk_getInstance]
35 // Get SDK instance
36 sdk = NCNavigineSdk.getInstance()
37 // [swift_NavigineSdk_getInstance]
38
39 guard let sdk = sdk else {
40 print("Error: failed to get NavigineSdk instance")
41 return
42 }
43
44 // [swift_NavigineSdk_getVersion]
45 // Get SDK version
46 print("Navigine SDK Version: \‍(NCNavigineSdk.getVersion())")
47 // [swift_NavigineSdk_getVersion]
48
49 // [swift_NavigineSdk_getDeviceId]
50 // Get device ID
51 print("Device ID: \‍(NCNavigineSdk.getDeviceId())")
52 // [swift_NavigineSdk_getDeviceId]
53
54 // [swift_NavigineSdk_getRelativeTime]
55 // Get relative time
56 print("Relative Time: \‍(NCNavigineSdk.getRelativeTime())")
57 // [swift_NavigineSdk_getRelativeTime]
58
59 // [swift_NavigineSdk_setUserHash]
60 // Set user hash (authorization token)
61 sdk.setUserHash("XXXX-XXXX-XXXX-XXXX")
62 // [swift_NavigineSdk_setUserHash]
63
64
65 // [swift_NavigineSdk_getUserAgent]
66 print("User-Agent: \‍(NCNavigineSdk.getUserAgent())")
67 // [swift_NavigineSdk_getUserAgent]
68
69 // [swift_NavigineSdk_reset]
70 sdk?.reset()
71 print("SDK reset to initial state")
72 // [swift_NavigineSdk_reset]
73
74 // [swift_NavigineSdk_setServer]
75 // Set server URL (optional)
76 sdk.setServer("https://custom.navigine.com")
77 // [swift_NavigineSdk_setServer]
78
79 // Initialize managers
80 initializeManagers()
81
82 } catch {
83 print("SDK initialization error: \‍(error)")
84 }
85 }
86
87 /**
88 * Initialize all SDK managers
89 */
90 private func initializeManagers() {
91 guard let sdk = sdk else { return }
92
93 // [swift_NavigineSdk_getLocationManager]
94 // Get LocationManager for working with locations
95 locationManager = sdk.getLocationManager()
96 if locationManager != nil {
97 print("LocationManager successfully initialized")
98 }
99 // [swift_NavigineSdk_getLocationManager]
100
101 // [swift_NavigineSdk_getNavigationManager]
102 // Get NavigationManager for navigation
103 if let locationManager = locationManager {
104 navigationManager = sdk.getNavigationManager(locationManager)
105 if navigationManager != nil {
106 print("NavigationManager successfully initialized")
107 }
108 }
109 // [swift_NavigineSdk_getNavigationManager]
110
111 // [swift_NavigineSdk_getZoneManager]
112 // Get ZoneManager for working with zones
113 if let navigationManager = navigationManager {
114 zoneManager = sdk.getZoneManager(navigationManager)
115 if zoneManager != nil {
116 print("ZoneManager successfully initialized")
117 }
118 }
119 // [swift_NavigineSdk_getZoneManager]
120
121 // [swift_NavigineSdk_getRouteManager]
122 // Get RouteManager for building routes
123 if let locationManager = locationManager,
124 let navigationManager = navigationManager {
125 routeManager = sdk.getRouteManager(locationManager, navigationManager: navigationManager)
126 if routeManager != nil {
127 print("RouteManager successfully initialized")
128 }
129 }
130 // [swift_NavigineSdk_getRouteManager]
131
132 // [swift_NavigineSdk_getAsyncRouteManager]
133 // Get AsyncRouteManager for async route operations
134 if let locationManager = locationManager,
135 let navigationManager = navigationManager {
136 asyncRouteManager = sdk.getAsyncRouteManager(locationManager, navigationManager: navigationManager)
137 if asyncRouteManager != nil {
138 print("AsyncRouteManager successfully initialized")
139 }
140 }
141 // [swift_NavigineSdk_getAsyncRouteManager]
142
143 // [swift_NavigineSdk_getNotificationManager]
144 // Get NotificationManager for notifications
145 if let locationManager = locationManager {
146 notificationManager = sdk.getNotificationManager(locationManager)
147 if notificationManager != nil {
148 print("NotificationManager successfully initialized")
149 }
150 }
151 // [swift_NavigineSdk_getNotificationManager]
152
153 // [swift_NavigineSdk_getMeasurementManager]
154 // Get MeasurementManager for measurements
155 if let locationManager = locationManager {
156 measurementManager = sdk.getMeasurementManager(locationManager)
157 if measurementManager != nil {
158 print("MeasurementManager successfully initialized")
159 }
160 }
161 // [swift_NavigineSdk_getMeasurementManager]
162
163 // [swift_NavigineSdk_getLocationListManager]
164 // Get LocationListManager for location list operations
165 locationListManager = sdk.getLocationListManager()
166 if locationListManager != nil {
167 print("LocationListManager successfully initialized")
168 }
169 // [swift_NavigineSdk_getLocationListManager]
170
171 // [swift_NavigineSdk_getStorageManager]
172 // Get StorageManager for working with storages
173 storageManager = sdk.getStorageManager()
174 if storageManager != nil {
175 print("StorageManager successfully initialized")
176 }
177 // [swift_NavigineSdk_getStorageManager]
178
179 // [swift_NavigineSdk_getRouteLayer]
180 // Create RouteLayer for building and drawing routes on the map
181 if let locationWindow = locationWindow {
182 routeLayer = sdk.getRouteLayer(locationWindow)
183 if routeLayer != nil {
184 print("RouteLayer successfully initialized")
185 }
186 }
187 // [swift_NavigineSdk_getRouteLayer]
188
189 // [swift_NavigineSdk_getGuidanceCamera]
190 if let locationWindow = locationWindow, let routeLayer = routeLayer {
191 guidanceCamera = sdk.getGuidanceCamera(locationWindow, routeLayer: routeLayer)
192 // [swift_GuidanceCamera_setMode]
193 guidanceCamera?.setMode(.FOLLOWING)
194 // [swift_GuidanceCamera_setMode]
195 // [swift_GuidanceCamera_set2DMode]
196 guidanceCamera?.set2DMode(false)
197 // [swift_GuidanceCamera_set2DMode]
198 // [swift_GuidanceCamera_mode]
199 print("Guidance mode: \‍(String(describing: guidanceCamera?.mode()))")
200 // [swift_GuidanceCamera_mode]
201 // [swift_GuidanceCamera_instruction]
202 if let next = guidanceCamera?.instruction() {
203 print("Next instruction: \‍(next.title ?? "") \‍(next.distance) m")
204 }
205 // [swift_GuidanceCamera_instruction]
206 }
207 // [swift_NavigineSdk_getGuidanceCamera]
208
209 // [swift_NavigineSdk_getRouteSimulator]
210 if let window = locationWindow {
211 let routeSimulator = sdk.getRouteSimulator(window)
212 // [swift_RouteSimulator_setGeometry]
213 let walk = NCLocationPolyline(points: [
214 NCGlobalPoint(latitude: 55.751, longitude: 37.618),
215 NCGlobalPoint(latitude: 55.752, longitude: 37.618),
216 ], sublocationId: nil)
217 _ = routeSimulator.setGeometry([walk])
218 // [swift_RouteSimulator_setGeometry]
219 // [swift_RouteSimulator_setSpeed]
220 routeSimulator.setSpeed(1.4)
221 // [swift_RouteSimulator_setSpeed]
222 // [swift_RouteSimulator_start]
223 let started = routeSimulator.start()
224 print("Route walk started: \‍(started)")
225 // [swift_RouteSimulator_start]
226 // [swift_RouteSimulator_sample]
227 if let sample = routeSimulator.sample() {
228 print("Walk advance: \‍(sample.advance) m")
229 }
230 // [swift_RouteSimulator_sample]
231 }
232 // [swift_NavigineSdk_getRouteSimulator]
233
234 // [swift_NavigineSdk_getMqttSession]
235 // Get MqttSession for publishing position data via MQTT
236 if let navigationManager = navigationManager {
237 mqttSession = sdk.getMqttSession(navigationManager)
238 if mqttSession != nil {
239 print("MqttSession successfully initialized")
240 }
241 }
242 // [swift_NavigineSdk_getMqttSession]
243 }
244
245 /**
246 * Get information about current SDK state
247 */
248 func printSdkInfo() {
249 print("=== Navigine SDK Information ===")
250 print("SDK Version: \‍(NCNavigineSdk.getVersion())")
251 print("Device ID: \‍(NCNavigineSdk.getDeviceId())")
252 print("Relative Time: \‍(NCNavigineSdk.getRelativeTime())")
253 print("LocationManager: \‍(locationManager != nil ? "available" : "unavailable")")
254 print("NavigationManager: \‍(navigationManager != nil ? "available" : "unavailable")")
255 print("ZoneManager: \‍(zoneManager != nil ? "available" : "unavailable")")
256 print("RouteManager: \‍(routeManager != nil ? "available" : "unavailable")")
257 print("AsyncRouteManager: \‍(asyncRouteManager != nil ? "available" : "unavailable")")
258 print("NotificationManager: \‍(notificationManager != nil ? "available" : "unavailable")")
259 print("MeasurementManager: \‍(measurementManager != nil ? "available" : "unavailable")")
260 print("LocationListManager: \‍(locationListManager != nil ? "available" : "unavailable")")
261 print("StorageManager: \‍(storageManager != nil ? "available" : "unavailable")")
262 print("MqttSession: \‍(mqttSession != nil ? "available" : "unavailable")")
263 }
264
265 /**
266 * Demonstrate usage of all managers
267 */
268 func demonstrateManagersUsage() {
269 print("=== Manager Usage Demonstration ===")
270
271 // LocationManager usage example
272 if let locationManager = locationManager {
273 print("LocationManager ready for location operations")
274 // Add code here for loading locations, working with them, etc.
275 }
276
277 // NavigationManager usage example
278 if let navigationManager = navigationManager {
279 print("NavigationManager ready for positioning")
280 // Add code here for getting position, setting up listeners, etc.
281 }
282
283 // ZoneManager usage example
284 if let zoneManager = zoneManager {
285 print("ZoneManager ready for zone operations")
286 // Add code here for setting up zones, handling enter/exit events, etc.
287 }
288
289 // RouteManager usage example
290 if let routeManager = routeManager {
291 print("RouteManager ready for route building")
292 // Add code here for building routes, setting destination points, etc.
293 }
294
295 // AsyncRouteManager usage example
296 if let asyncRouteManager = asyncRouteManager {
297 print("AsyncRouteManager ready for async route operations")
298 // Add code here for creating routing sessions, etc.
299 }
300
301 // NotificationManager usage example
302 if let notificationManager = notificationManager {
303 print("NotificationManager ready for notifications")
304 // Add code here for setting up beacon notifications, etc.
305 }
306
307 // MeasurementManager usage example
308 if let measurementManager = measurementManager {
309 print("MeasurementManager ready for measurements")
310 // Add code here for managing measurement generators, etc.
311 }
312
313 // LocationListManager usage example
314 if let locationListManager = locationListManager {
315 print("LocationListManager ready for location list operations")
316 // Add code here for getting available locations list, etc.
317 }
318
319 // StorageManager usage example
320 if let storageManager = storageManager {
321 print("StorageManager ready for storage operations")
322 // Add code here for creating storages, managing data, etc.
323 }
324
325 // MqttSession usage example
326 if let mqttSession = mqttSession {
327 print("MqttSession ready for publishing position data via MQTT")
328 // Add code here for connecting to MQTT broker, publishing positions, etc.
329 }
330 }
331
332 /**
333 * Demonstrate Swift features usage
334 */
335 func demonstrateSwiftFeatures() {
336 print("=== Swift Features Demonstration ===")
337
338 // Using guard let for safe optional unwrapping
339 guard let sdk = sdk else {
340 print("SDK unavailable")
341 return
342 }
343
344 // Using defer for cleanup code execution
345 defer {
346 print("Swift features demonstration completed")
347 }
348
349 // Using map for data transformation
350 let sdkInfo = [
351 "version": NCNavigineSdk.getVersion(),
352 "deviceId": NCNavigineSdk.getDeviceId(),
353 "relativeTime": NCNavigineSdk.getRelativeTime()
354 ]
355
356 let infoStrings = sdkInfo.map { key, value in
357 "\‍(key): \‍(value)"
358 }
359 print("SDK Information: \‍(infoStrings.joined(separator: ", "))")
360
361 // Using compactMap for nil filtering
362 let managers = [
363 locationManager,
364 navigationManager,
365 zoneManager,
366 routeManager,
367 asyncRouteManager,
368 notificationManager,
369 measurementManager,
370 locationListManager,
371 storageManager,
372 mqttSession
373 ]
374
375 let availableManagers = managers.compactMap { $0 }
376 print("Available managers: \‍(availableManagers.count)")
377
378 // Using async/await (if supported)
379 Task {
380 await performAsyncOperation()
381 }
382 }
383
384 /**
385 * Async operation
386 */
387 @MainActor
388 private func performAsyncOperation() async {
389 // Simulate async operation
390 try? await Task.sleep(nanoseconds: 1_000_000_000) // 1 second
391 print("Async operation completed")
392 }
393
394 /**
395 * Demonstrate property wrappers usage
396 */
397 func demonstratePropertyWrappers() {
398 print("=== Property Wrappers Demonstration ===")
399
400 // Example of @Published usage (if this was an ObservableObject)
401 // @Published var sdkVersion = NCNavigineSdk.getVersion()
402
403 // Example of @State usage (for SwiftUI)
404 // @State private var isInitialized = false
405
406 print("Property wrappers demonstrated")
407 }
408
409 /**
410 * Main method for demonstration
411 */
412 func runExample() {
413 printSdkInfo()
414 demonstrateManagersUsage()
415 demonstrateSwiftFeatures()
416 demonstratePropertyWrappers()
417 }
418}
419
420/**
421 * Extension for additional functionality
422 */
423extension NavigineSdkExample {
424
425 /**
426 * Get status of all managers
427 */
428 func getManagersStatus() -> [String: Bool] {
429 return [
430 "LocationManager": locationManager != nil,
431 "NavigationManager": navigationManager != nil,
432 "ZoneManager": zoneManager != nil,
433 "RouteManager": routeManager != nil,
434 "AsyncRouteManager": asyncRouteManager != nil,
435 "NotificationManager": notificationManager != nil,
436 "MeasurementManager": measurementManager != nil,
437 "LocationListManager": locationListManager != nil,
438 "StorageManager": storageManager != nil,
439 "MqttSession": mqttSession != nil
440 ]
441 }
442
443 /**
444 * Check if SDK is ready for work
445 */
446 func isSdkReady() -> Bool {
447 return sdk != nil && locationManager != nil
448 }
449}
450
451/**
452 * Function to run the example
453 */
454func main() {
455 let example = NavigineSdkExample()
456 example.runExample()
457}
458
459// Run the example
460main()