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
21 init() {
22 initializeSdk()
23 }
24
25 /**
26 * SDK initialization and basic parameter setup
27 */
28 private func initializeSdk() {
29 do {
30 // [swift_NavigineSdk_getInstance]
31 // Get SDK instance
32 sdk = NCNavigineSdk.getInstance()
33 // [swift_NavigineSdk_getInstance]
34
35 guard let sdk = sdk else {
36 print("Error: failed to get NavigineSdk instance")
37 return
38 }
39
40 // [swift_NavigineSdk_getVersion]
41 // Get SDK version
42 print("Navigine SDK Version: \‍(NCNavigineSdk.getVersion())")
43 // [swift_NavigineSdk_getVersion]
44
45 // [swift_NavigineSdk_getDeviceId]
46 // Get device ID
47 print("Device ID: \‍(NCNavigineSdk.getDeviceId())")
48 // [swift_NavigineSdk_getDeviceId]
49
50 // [swift_NavigineSdk_getRelativeTime]
51 // Get relative time
52 print("Relative Time: \‍(NCNavigineSdk.getRelativeTime())")
53 // [swift_NavigineSdk_getRelativeTime]
54
55 // [swift_NavigineSdk_setUserHash]
56 // Set user hash (authorization token)
57 sdk.setUserHash("XXXX-XXXX-XXXX-XXXX")
58 // [swift_NavigineSdk_setUserHash]
59
60 // [swift_NavigineSdk_setServer]
61 // Set server URL (optional)
62 sdk.setServer("https://custom.navigine.com")
63 // [swift_NavigineSdk_setServer]
64
65 // Initialize managers
66 initializeManagers()
67
68 } catch {
69 print("SDK initialization error: \‍(error)")
70 }
71 }
72
73 /**
74 * Initialize all SDK managers
75 */
76 private func initializeManagers() {
77 guard let sdk = sdk else { return }
78
79 // [swift_NavigineSdk_getLocationManager]
80 // Get LocationManager for working with locations
81 locationManager = sdk.getLocationManager()
82 if locationManager != nil {
83 print("LocationManager successfully initialized")
84 }
85 // [swift_NavigineSdk_getLocationManager]
86
87 // [swift_NavigineSdk_getNavigationManager]
88 // Get NavigationManager for navigation
89 if let locationManager = locationManager {
90 navigationManager = sdk.getNavigationManager(locationManager)
91 if navigationManager != nil {
92 print("NavigationManager successfully initialized")
93 }
94 }
95 // [swift_NavigineSdk_getNavigationManager]
96
97 // [swift_NavigineSdk_getZoneManager]
98 // Get ZoneManager for working with zones
99 if let navigationManager = navigationManager {
100 zoneManager = sdk.getZoneManager(navigationManager)
101 if zoneManager != nil {
102 print("ZoneManager successfully initialized")
103 }
104 }
105 // [swift_NavigineSdk_getZoneManager]
106
107 // [swift_NavigineSdk_getRouteManager]
108 // Get RouteManager for building routes
109 if let locationManager = locationManager,
110 let navigationManager = navigationManager {
111 routeManager = sdk.getRouteManager(locationManager, navigationManager: navigationManager)
112 if routeManager != nil {
113 print("RouteManager successfully initialized")
114 }
115 }
116 // [swift_NavigineSdk_getRouteManager]
117
118 // [swift_NavigineSdk_getAsyncRouteManager]
119 // Get AsyncRouteManager for async route operations
120 if let locationManager = locationManager,
121 let navigationManager = navigationManager {
122 asyncRouteManager = sdk.getAsyncRouteManager(locationManager, navigationManager: navigationManager)
123 if asyncRouteManager != nil {
124 print("AsyncRouteManager successfully initialized")
125 }
126 }
127 // [swift_NavigineSdk_getAsyncRouteManager]
128
129 // [swift_NavigineSdk_getNotificationManager]
130 // Get NotificationManager for notifications
131 if let locationManager = locationManager {
132 notificationManager = sdk.getNotificationManager(locationManager)
133 if notificationManager != nil {
134 print("NotificationManager successfully initialized")
135 }
136 }
137 // [swift_NavigineSdk_getNotificationManager]
138
139 // [swift_NavigineSdk_getMeasurementManager]
140 // Get MeasurementManager for measurements
141 if let locationManager = locationManager {
142 measurementManager = sdk.getMeasurementManager(locationManager)
143 if measurementManager != nil {
144 print("MeasurementManager successfully initialized")
145 }
146 }
147 // [swift_NavigineSdk_getMeasurementManager]
148
149 // [swift_NavigineSdk_getLocationListManager]
150 // Get LocationListManager for location list operations
151 locationListManager = sdk.getLocationListManager()
152 if locationListManager != nil {
153 print("LocationListManager successfully initialized")
154 }
155 // [swift_NavigineSdk_getLocationListManager]
156
157 // [swift_NavigineSdk_getStorageManager]
158 // Get StorageManager for working with storages
159 storageManager = sdk.getStorageManager()
160 if storageManager != nil {
161 print("StorageManager successfully initialized")
162 }
163 // [swift_NavigineSdk_getStorageManager]
164 }
165
166 /**
167 * Get information about current SDK state
168 */
169 func printSdkInfo() {
170 print("=== Navigine SDK Information ===")
171 print("SDK Version: \‍(NCNavigineSdk.getVersion())")
172 print("Device ID: \‍(NCNavigineSdk.getDeviceId())")
173 print("Relative Time: \‍(NCNavigineSdk.getRelativeTime())")
174 print("LocationManager: \‍(locationManager != nil ? "available" : "unavailable")")
175 print("NavigationManager: \‍(navigationManager != nil ? "available" : "unavailable")")
176 print("ZoneManager: \‍(zoneManager != nil ? "available" : "unavailable")")
177 print("RouteManager: \‍(routeManager != nil ? "available" : "unavailable")")
178 print("AsyncRouteManager: \‍(asyncRouteManager != nil ? "available" : "unavailable")")
179 print("NotificationManager: \‍(notificationManager != nil ? "available" : "unavailable")")
180 print("MeasurementManager: \‍(measurementManager != nil ? "available" : "unavailable")")
181 print("LocationListManager: \‍(locationListManager != nil ? "available" : "unavailable")")
182 print("StorageManager: \‍(storageManager != nil ? "available" : "unavailable")")
183 }
184
185 /**
186 * Demonstrate usage of all managers
187 */
188 func demonstrateManagersUsage() {
189 print("=== Manager Usage Demonstration ===")
190
191 // LocationManager usage example
192 if let locationManager = locationManager {
193 print("LocationManager ready for location operations")
194 // Add code here for loading locations, working with them, etc.
195 }
196
197 // NavigationManager usage example
198 if let navigationManager = navigationManager {
199 print("NavigationManager ready for positioning")
200 // Add code here for getting position, setting up listeners, etc.
201 }
202
203 // ZoneManager usage example
204 if let zoneManager = zoneManager {
205 print("ZoneManager ready for zone operations")
206 // Add code here for setting up zones, handling enter/exit events, etc.
207 }
208
209 // RouteManager usage example
210 if let routeManager = routeManager {
211 print("RouteManager ready for route building")
212 // Add code here for building routes, setting destination points, etc.
213 }
214
215 // AsyncRouteManager usage example
216 if let asyncRouteManager = asyncRouteManager {
217 print("AsyncRouteManager ready for async route operations")
218 // Add code here for creating routing sessions, etc.
219 }
220
221 // NotificationManager usage example
222 if let notificationManager = notificationManager {
223 print("NotificationManager ready for notifications")
224 // Add code here for setting up beacon notifications, etc.
225 }
226
227 // MeasurementManager usage example
228 if let measurementManager = measurementManager {
229 print("MeasurementManager ready for measurements")
230 // Add code here for managing measurement generators, etc.
231 }
232
233 // LocationListManager usage example
234 if let locationListManager = locationListManager {
235 print("LocationListManager ready for location list operations")
236 // Add code here for getting available locations list, etc.
237 }
238
239 // StorageManager usage example
240 if let storageManager = storageManager {
241 print("StorageManager ready for storage operations")
242 // Add code here for creating storages, managing data, etc.
243 }
244 }
245
246 /**
247 * Demonstrate Swift features usage
248 */
249 func demonstrateSwiftFeatures() {
250 print("=== Swift Features Demonstration ===")
251
252 // Using guard let for safe optional unwrapping
253 guard let sdk = sdk else {
254 print("SDK unavailable")
255 return
256 }
257
258 // Using defer for cleanup code execution
259 defer {
260 print("Swift features demonstration completed")
261 }
262
263 // Using map for data transformation
264 let sdkInfo = [
265 "version": NCNavigineSdk.getVersion(),
266 "deviceId": NCNavigineSdk.getDeviceId(),
267 "relativeTime": NCNavigineSdk.getRelativeTime()
268 ]
269
270 let infoStrings = sdkInfo.map { key, value in
271 "\‍(key): \‍(value)"
272 }
273 print("SDK Information: \‍(infoStrings.joined(separator: ", "))")
274
275 // Using compactMap for nil filtering
276 let managers = [
277 locationManager,
278 navigationManager,
279 zoneManager,
280 routeManager,
281 asyncRouteManager,
282 notificationManager,
283 measurementManager,
284 locationListManager,
285 storageManager
286 ]
287
288 let availableManagers = managers.compactMap { $0 }
289 print("Available managers: \‍(availableManagers.count)")
290
291 // Using async/await (if supported)
292 Task {
293 await performAsyncOperation()
294 }
295 }
296
297 /**
298 * Async operation
299 */
300 @MainActor
301 private func performAsyncOperation() async {
302 // Simulate async operation
303 try? await Task.sleep(nanoseconds: 1_000_000_000) // 1 second
304 print("Async operation completed")
305 }
306
307 /**
308 * Demonstrate property wrappers usage
309 */
310 func demonstratePropertyWrappers() {
311 print("=== Property Wrappers Demonstration ===")
312
313 // Example of @Published usage (if this was an ObservableObject)
314 // @Published var sdkVersion = NCNavigineSdk.getVersion()
315
316 // Example of @State usage (for SwiftUI)
317 // @State private var isInitialized = false
318
319 print("Property wrappers demonstrated")
320 }
321
322 /**
323 * Main method for demonstration
324 */
325 func runExample() {
326 printSdkInfo()
327 demonstrateManagersUsage()
328 demonstrateSwiftFeatures()
329 demonstratePropertyWrappers()
330 }
331}
332
333/**
334 * Extension for additional functionality
335 */
336extension NavigineSdkExample {
337
338 /**
339 * Get status of all managers
340 */
341 func getManagersStatus() -> [String: Bool] {
342 return [
343 "LocationManager": locationManager != nil,
344 "NavigationManager": navigationManager != nil,
345 "ZoneManager": zoneManager != nil,
346 "RouteManager": routeManager != nil,
347 "AsyncRouteManager": asyncRouteManager != nil,
348 "NotificationManager": notificationManager != nil,
349 "MeasurementManager": measurementManager != nil,
350 "LocationListManager": locationListManager != nil
351 ]
352 }
353
354 /**
355 * Check if SDK is ready for work
356 */
357 func isSdkReady() -> Bool {
358 return sdk != nil && locationManager != nil
359 }
360}
361
362/**
363 * Function to run the example
364 */
365func main() {
366 let example = NavigineSdkExample()
367 example.runExample()
368}
369
370// Run the example
371main()