|
| 1 | +import Foundation |
| 2 | +import CascableCore |
| 3 | + |
| 4 | +public extension HotspotHelpers { |
| 5 | + |
| 6 | + /// Attempt to join the given WiFi network. |
| 7 | + /// |
| 8 | + /// - Note: Joining a WiFi network can take upwards of 20-30 seconds. |
| 9 | + /// |
| 10 | + /// - Parameters: |
| 11 | + /// - ssid: The SSID of the network to join. |
| 12 | + /// - password: The password of the network to join. |
| 13 | + /// - promptForLocation: If the current platform requires location permission to connect to a network, pass `true` |
| 14 | + /// to automatically prompt for that permission if needed. |
| 15 | + /// - completionHandler: The completion handler to be called with the result of the operation. |
| 16 | + /// - completionQueue: The queue on which to call the completion handler. |
| 17 | + func attemptToJoinWiFiNetwork(_ ssid: String, password: String, |
| 18 | + promptingForLocationIfNeeded promptForLocation: Bool = true, |
| 19 | + completionHandler: @escaping (Result<HotspotConfiguration, Error>) -> Void, |
| 20 | + completionQueue: DispatchQueue) { |
| 21 | + |
| 22 | + attemptToJoinWiFiNetwork(ssid, password: password, promptingForLocationIfNeeded: promptForLocation, |
| 23 | + completionHandler: { config, error in |
| 24 | + if let config { |
| 25 | + completionHandler(.success(config)) |
| 26 | + } else if let error { |
| 27 | + completionHandler(.failure(error)) |
| 28 | + } else { |
| 29 | + completionHandler(.failure(NSError(cblErrorCode: .networkChangeFailed))) |
| 30 | + } |
| 31 | + }, completionQueue: completionQueue) |
| 32 | + } |
| 33 | + |
| 34 | + /// Attempt to join the given WiFi network. |
| 35 | + /// |
| 36 | + /// - Note: Joining a WiFi network can take upwards of 20-30 seconds. |
| 37 | + /// |
| 38 | + /// - Parameters: |
| 39 | + /// - ssid: The SSID of the network to join. |
| 40 | + /// - password: The password of the network to join. |
| 41 | + /// - promptForLocation: If the current platform requires location permission to connect to a network, pass `true` |
| 42 | + /// to automatically prompt for that permission if needed. |
| 43 | + /// - Returns: Upon successful connection, returns a `HotspotConfiguration` object describing the network. |
| 44 | + func attemptToJoinWiFiNetwork(_ ssid: String, password: String, |
| 45 | + promptingForLocationIfNeeded promptForLocation: Bool = true) async throws -> HotspotConfiguration { |
| 46 | + |
| 47 | + return try await withCheckedThrowingContinuation({ continuation in |
| 48 | + attemptToJoinWiFiNetwork(ssid, password: password, promptingForLocationIfNeeded: promptForLocation, |
| 49 | + completionHandler: { result in |
| 50 | + continuation.resume(with: result) |
| 51 | + }, completionQueue: .main) |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + /// Attempt to join the WiFi network described by the given QR code scanning result. See `CameraQRDecoding` for details. |
| 56 | + /// |
| 57 | + /// - Note: Joining a WiFi network can take upwards of 20-30 seconds. |
| 58 | + /// |
| 59 | + /// - Parameters: |
| 60 | + /// - networkDetails The details of the network to join. |
| 61 | + /// - promptForLocation: If the current platform requires location permission to connect to a network, pass `true` |
| 62 | + /// to automatically prompt for that permission if needed. |
| 63 | + /// - completionHandler: The completion handler to be called with the result of the operation. |
| 64 | + /// - completionQueue: The queue on which to call the completion handler. |
| 65 | + func attemptToJoinQRCodeNetwork(_ networkDetails: CameraWiFiDetails, |
| 66 | + promptingForLocationIfNeeded promptForLocation: Bool = true, |
| 67 | + completionHandler: @escaping (Result<HotspotConfiguration, Error>) -> Void, |
| 68 | + completionQueue: DispatchQueue) { |
| 69 | + |
| 70 | + attemptToJoinQRCodeNetwork(networkDetails, promptingForLocationIfNeeded: promptForLocation, |
| 71 | + completionHandler: { config, error in |
| 72 | + if let config { |
| 73 | + completionHandler(.success(config)) |
| 74 | + } else if let error { |
| 75 | + completionHandler(.failure(error)) |
| 76 | + } else { |
| 77 | + completionHandler(.failure(NSError(cblErrorCode: .networkChangeFailed))) |
| 78 | + } |
| 79 | + }, completionQueue: completionQueue) |
| 80 | + } |
| 81 | + |
| 82 | + /// Attempt to join the WiFi network described by the given QR code scanning result. See `CameraQRDecoding` for details. |
| 83 | + /// |
| 84 | + /// - Note: Joining a WiFi network can take upwards of 20-30 seconds. |
| 85 | + /// |
| 86 | + /// - Parameters: |
| 87 | + /// - networkDetails The details of the network to join. |
| 88 | + /// - promptForLocation: If the current platform requires location permission to connect to a network, pass `true` |
| 89 | + /// to automatically prompt for that permission if needed. |
| 90 | + /// - Returns: Upon successful connection, returns a `HotspotConfiguration` object describing the network. |
| 91 | + func attemptToJoinQRCodeNetwork(_ networkDetails: CameraWiFiDetails, |
| 92 | + promptingForLocationIfNeeded promptForLocation: Bool = true) async throws -> HotspotConfiguration { |
| 93 | + |
| 94 | + return try await withCheckedThrowingContinuation({ continuation in |
| 95 | + attemptToJoinQRCodeNetwork(networkDetails, promptingForLocationIfNeeded: promptForLocation, |
| 96 | + completionHandler: { result in |
| 97 | + continuation.resume(with: result) |
| 98 | + }, completionQueue: .main) |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
0 commit comments