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?
21 private var locationWindow: NCLocationWindow?
22 private var routeLayer: NCRouteLayer?
23 private var guidanceCamera: NCGuidanceCamera?
30 * SDK initialization and basic parameter setup
32 private func initializeSdk() {
34 // [swift_NavigineSdk_getInstance]
36 sdk = NCNavigineSdk.getInstance()
37 // [swift_NavigineSdk_getInstance]
39 guard let sdk = sdk else {
40 print("Error: failed to get NavigineSdk instance")
44 // [swift_NavigineSdk_getVersion]
46 print("Navigine SDK Version: \(NCNavigineSdk.getVersion())")
47 // [swift_NavigineSdk_getVersion]
49 // [swift_NavigineSdk_getDeviceId]
51 print("Device ID: \(NCNavigineSdk.getDeviceId())")
52 // [swift_NavigineSdk_getDeviceId]
54 // [swift_NavigineSdk_getRelativeTime]
56 print("Relative Time: \(NCNavigineSdk.getRelativeTime())")
57 // [swift_NavigineSdk_getRelativeTime]
59 // [swift_NavigineSdk_setUserHash]
60 // Set user hash (authorization token)
61 sdk.setUserHash("XXXX-XXXX-XXXX-XXXX")
62 // [swift_NavigineSdk_setUserHash]
65 // [swift_NavigineSdk_getUserAgent]
66 print("User-Agent: \(NCNavigineSdk.getUserAgent())")
67 // [swift_NavigineSdk_getUserAgent]
69 // [swift_NavigineSdk_reset]
71 print("SDK reset to initial state")
72 // [swift_NavigineSdk_reset]
74 // [swift_NavigineSdk_setServer]
75 // Set server URL (optional)
76 sdk.setServer("https://custom.navigine.com")
77 // [swift_NavigineSdk_setServer]
79 // Initialize managers
83 print("SDK initialization error: \(error)")
88 * Initialize all SDK managers
90 private func initializeManagers() {
91 guard let sdk = sdk else { return }
93 // [swift_NavigineSdk_getLocationManager]
94 // Get LocationManager for working with locations
95 locationManager = sdk.getLocationManager()
96 if locationManager != nil {
97 print("LocationManager successfully initialized")
99 // [swift_NavigineSdk_getLocationManager]
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")
109 // [swift_NavigineSdk_getNavigationManager]
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")
119 // [swift_NavigineSdk_getZoneManager]
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")
130 // [swift_NavigineSdk_getRouteManager]
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")
141 // [swift_NavigineSdk_getAsyncRouteManager]
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")
151 // [swift_NavigineSdk_getNotificationManager]
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")
161 // [swift_NavigineSdk_getMeasurementManager]
163 // [swift_NavigineSdk_getLocationListManager]
164 // Get LocationListManager for location list operations
165 locationListManager = sdk.getLocationListManager()
166 if locationListManager != nil {
167 print("LocationListManager successfully initialized")
169 // [swift_NavigineSdk_getLocationListManager]
171 // [swift_NavigineSdk_getStorageManager]
172 // Get StorageManager for working with storages
173 storageManager = sdk.getStorageManager()
174 if storageManager != nil {
175 print("StorageManager successfully initialized")
177 // [swift_NavigineSdk_getStorageManager]
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")
187 // [swift_NavigineSdk_getRouteLayer]
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")
205 // [swift_GuidanceCamera_instruction]
207 // [swift_NavigineSdk_getGuidanceCamera]
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")
230 // [swift_RouteSimulator_sample]
232 // [swift_NavigineSdk_getRouteSimulator]
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")
242 // [swift_NavigineSdk_getMqttSession]
246 * Get information about current SDK state
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")")
266 * Demonstrate usage of all managers
268 func demonstrateManagersUsage() {
269 print("=== Manager Usage Demonstration ===")
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.
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.
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.
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.
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.
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.
307 // MeasurementManager usage example
308 if let measurementManager = measurementManager {
309 print("MeasurementManager ready for measurements")
310 // Add code here for managing measurement generators, etc.
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.
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.
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.
333 * Demonstrate Swift features usage
335 func demonstrateSwiftFeatures() {
336 print("=== Swift Features Demonstration ===")
338 // Using guard let for safe optional unwrapping
339 guard let sdk = sdk else {
340 print("SDK unavailable")
344 // Using defer for cleanup code execution
346 print("Swift features demonstration completed")
349 // Using map for data transformation
351 "version": NCNavigineSdk.getVersion(),
352 "deviceId": NCNavigineSdk.getDeviceId(),
353 "relativeTime": NCNavigineSdk.getRelativeTime()
356 let infoStrings = sdkInfo.map { key, value in
359 print("SDK Information: \(infoStrings.joined(separator: ", "))")
361 // Using compactMap for nil filtering
375 let availableManagers = managers.compactMap { $0 }
376 print("Available managers: \(availableManagers.count)")
378 // Using async/await (if supported)
380 await performAsyncOperation()
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")
395 * Demonstrate property wrappers usage
397 func demonstratePropertyWrappers() {
398 print("=== Property Wrappers Demonstration ===")
400 // Example of @Published usage (if this was an ObservableObject)
401 // @Published var sdkVersion = NCNavigineSdk.getVersion()
403 // Example of @State usage (for SwiftUI)
404 // @State private var isInitialized = false
406 print("Property wrappers demonstrated")
410 * Main method for demonstration
414 demonstrateManagersUsage()
415 demonstrateSwiftFeatures()
416 demonstratePropertyWrappers()
421 * Extension for additional functionality
423extension NavigineSdkExample {
426 * Get status of all managers
428 func getManagersStatus() -> [String: Bool] {
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
444 * Check if SDK is ready for work
446 func isSdkReady() -> Bool {
447 return sdk != nil && locationManager != nil
452 * Function to run the example
455 let example = NavigineSdkExample()