|
29 | 29 |
|
30 | 30 | #if canImport(WatchConnectivity) |
31 | 31 | public import Foundation |
| 32 | + #if canImport(os) |
| 33 | + import os.log |
| 34 | + #endif |
32 | 35 | public import SundialKitCore |
33 | 36 | import WatchConnectivity |
34 | 37 |
|
|
76 | 79 | return ConnectivityMessage(forceCasting: context) |
77 | 80 | } |
78 | 81 |
|
| 82 | + /// The number of queued user-info transfers not yet delivered. |
| 83 | + public var outstandingUserInfoTransferCount: Int { |
| 84 | + session.outstandingUserInfoTransfers.count |
| 85 | + } |
| 86 | + |
| 87 | + /// The number of queued file transfers not yet delivered. |
| 88 | + public var outstandingFileTransferCount: Int { |
| 89 | + session.outstandingFileTransfers.count |
| 90 | + } |
| 91 | + |
79 | 92 | /// Updates the application context to be sent to the counterpart device. |
80 | 93 | /// |
81 | 94 | /// The context is delivered opportunistically when the counterpart wakes up. |
|
133 | 146 | } |
134 | 147 | } |
135 | 148 |
|
| 149 | + /// Queues a dictionary for background delivery to the counterpart device. |
| 150 | + /// |
| 151 | + /// Backed by `WCSession.transferUserInfo(_:)`. The returned transfer handle |
| 152 | + /// is ignored for now. |
| 153 | + /// |
| 154 | + /// - Parameter userInfo: The dictionary to queue for delivery |
| 155 | + public func transferUserInfo(_ userInfo: ConnectivityMessage) { |
| 156 | + _ = session.transferUserInfo(userInfo as [String: Any]) |
| 157 | + } |
| 158 | + |
| 159 | + /// Queues binary data for background delivery to the counterpart device. |
| 160 | + /// |
| 161 | + /// The encoded `Data` is written to a uniquely named temporary file, which |
| 162 | + /// is then handed to `WCSession.transferFile(_:metadata:)`. The temporary |
| 163 | + /// file is removed once the transfer finishes (see |
| 164 | + /// `session(_:didFinish:error:)`). The returned transfer handle is ignored |
| 165 | + /// for now. |
| 166 | + /// |
| 167 | + /// - Parameters: |
| 168 | + /// - fileData: The encoded binary data to transfer |
| 169 | + /// - metadata: Optional dictionary describing the payload |
| 170 | + public func transferFile(_ fileData: Data, metadata: ConnectivityMessage?) { |
| 171 | + let tempURL = FileManager.default.temporaryDirectory |
| 172 | + .appendingPathComponent(UUID().uuidString) |
| 173 | + do { |
| 174 | + try fileData.write(to: tempURL) |
| 175 | + } catch { |
| 176 | + if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) { |
| 177 | + SundialLogger.connectivity.error( |
| 178 | + "Failed to write temp file for transferFile: \(error.localizedDescription)" |
| 179 | + ) |
| 180 | + } |
| 181 | + return |
| 182 | + } |
| 183 | + _ = session.transferFile(tempURL, metadata: metadata as [String: Any]?) |
| 184 | + } |
| 185 | + |
136 | 186 | /// Activates the session to begin communication with the counterpart device. |
137 | 187 | /// |
138 | 188 | /// Must be called before any message exchange can occur. |
|
0 commit comments