@blotch/angular-lynx-native 22.3.0 → 22.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -15
- package/dist/android/project.d.ts +90 -1
- package/dist/android/project.d.ts.map +1 -1
- package/dist/android/project.js +369 -5
- package/dist/eject.d.ts +15 -1
- package/dist/eject.d.ts.map +1 -1
- package/dist/eject.js +13 -2
- package/dist/features.d.ts +70 -0
- package/dist/features.d.ts.map +1 -0
- package/dist/features.js +115 -0
- package/dist/generate.d.ts +17 -1
- package/dist/generate.d.ts.map +1 -1
- package/dist/generate.js +12 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/ios/build.d.ts.map +1 -1
- package/dist/ios/build.js +8 -2
- package/dist/ios/frameworks.d.ts +6 -5
- package/dist/ios/frameworks.d.ts.map +1 -1
- package/dist/ios/frameworks.js +46 -15
- package/dist/ios/project.d.ts +173 -11
- package/dist/ios/project.d.ts.map +1 -1
- package/dist/ios/project.js +767 -33
- package/dist/options.d.ts +143 -2
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +127 -0
- package/dist/permissions.d.ts +18 -1
- package/dist/permissions.d.ts.map +1 -1
- package/dist/permissions.js +63 -3
- package/package.json +1 -1
- package/templates/android/README.md +37 -2
- package/templates/android/angular-lynx.properties +2 -0
- package/templates/android/app/build.gradle.kts +70 -1
- package/templates/android/app/src/main/AndroidManifest.xml +24 -3
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BarcodeScannerModule.kt +144 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BiometricsModule.kt +433 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapPresenter.kt +98 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapRendererContext.kt +175 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BluetoothModule.kt +1288 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/CalendarModule.kt +127 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DContext.kt +1557 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DOps.kt +63 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasFilter.kt +357 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +4 -3
- package/templates/android/app/src/main/java/dev/angularlynx/shell/FilePickerModule.kt +263 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/HapticsModule.kt +82 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +376 -6
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ModuleBytes.kt +58 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/MotionModule.kt +332 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/NfcModule.kt +274 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +25 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +168 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShareTargetModule.kt +234 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +6 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +47 -3
- package/templates/android/app/src/main/java/dev/angularlynx/shell/StoreReviewModule.kt +66 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/SystemFontFamilies.kt +68 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/TrackingModule.kt +74 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoFrameTarget.kt +429 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoUI.kt +676 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +128 -13
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +493 -65
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WifiModule.kt +429 -0
- package/templates/android/app/src/main/res/values/biometrics.xml +9 -0
- package/templates/android/app/src/main/res/values/share_target.xml +11 -0
- package/templates/android/app/src/main/res/xml/backup_rules.xml +5 -3
- package/templates/android/app/src/main/res/xml/data_extraction_rules.xml +6 -3
- package/templates/android/build.gradle.kts +1 -1
- package/templates/android/gradle/wrapper/gradle-wrapper.properties +2 -2
- package/templates/ios/App/AngularLynxBitmapRendererContext.m +188 -0
- package/templates/ios/App/AngularLynxCanvas2DContext.m +2467 -0
- package/templates/ios/App/AngularLynxCanvas2DOps.h +61 -0
- package/templates/ios/App/AngularLynxVideo.h +37 -0
- package/templates/ios/App/AngularLynxVideo.m +435 -0
- package/templates/ios/App/AngularLynxWebGL.h +69 -7
- package/templates/ios/App/AngularLynxWebGL.m +538 -29
- package/templates/ios/App/AngularLynxWebGLContext.m +148 -46
- package/templates/ios/App/AppDelegate.swift +34 -2
- package/templates/ios/App/AppleSignInButton.swift +106 -0
- package/templates/ios/App/AppleSignInModule.swift +222 -0
- package/templates/ios/App/BarcodeScannerModule.swift +284 -0
- package/templates/ios/App/BiometricsModule.swift +256 -0
- package/templates/ios/App/BluetoothModule.swift +1086 -0
- package/templates/ios/App/CalendarModule.swift +158 -0
- package/templates/ios/App/FilePickerModule.swift +240 -0
- package/templates/ios/App/HapticsModule.swift +90 -0
- package/templates/ios/App/Info.plist +2 -0
- package/templates/ios/App/LynxViewController.swift +210 -6
- package/templates/ios/App/MotionModule.swift +430 -0
- package/templates/ios/App/NfcModule.swift +346 -0
- package/templates/ios/App/PhotoLibraryModule.swift +169 -0
- package/templates/ios/App/ShareTargetModule.swift +150 -0
- package/templates/ios/App/ShellModule.swift +17 -0
- package/templates/ios/App/StoreReviewModule.swift +61 -0
- package/templates/ios/App/TrackingModule.swift +121 -0
- package/templates/ios/App/WifiModule.swift +237 -0
- package/templates/ios/App/XElementRegistry.swift +13 -3
- package/templates/ios/ShareExtension/Info.plist +42 -0
- package/templates/ios/ShareExtension/ShareViewController.swift +387 -0
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import CoreNFC
|
|
2
|
+
import Lynx
|
|
3
|
+
|
|
4
|
+
/// The native module behind the runtime's `LynxNfc`, which calls it as
|
|
5
|
+
/// `NativeModules.AngularLynxNfc` (from the background thread, where
|
|
6
|
+
/// NativeModules lives). Android's shell has one of the same name.
|
|
7
|
+
///
|
|
8
|
+
/// A session is Core NFC's `NFCNDEFReaderSession`, which shows the system's
|
|
9
|
+
/// scan sheet while it's open. The runtime opens one with `start`, takes
|
|
10
|
+
/// each tag with `next`, and closes it with `stop`:
|
|
11
|
+
///
|
|
12
|
+
/// - `state` calls back `{ state }`: `ready` on an iPhone that reads tags,
|
|
13
|
+
/// `unsupported` on one that doesn't (an iPad, the Simulator).
|
|
14
|
+
/// - `start` takes `{ alertMessage?, multiple, write? }` and calls back `{}`
|
|
15
|
+
/// once the sheet is up, or `{ error, message }`. With `write`, a list of
|
|
16
|
+
/// records, each tag found is written to rather than read.
|
|
17
|
+
/// - `next` calls back once with the next tag, `{ tag }`, or
|
|
18
|
+
/// `{ error, message }`: a tag that couldn't be read or written (only when
|
|
19
|
+
/// `multiple` is false: a scan skips it and says so on the sheet), or why
|
|
20
|
+
/// the session ended, `stopped` when the runtime ended it.
|
|
21
|
+
/// - `stop` takes `{ message?, errorMessage? }`, what the sheet says as it
|
|
22
|
+
/// closes, and calls back `{}`.
|
|
23
|
+
///
|
|
24
|
+
/// Records cross as `{ tnf, type, id, payload }`, the last three base64: the
|
|
25
|
+
/// runtime builds and reads them, so both shells treat records the same way.
|
|
26
|
+
///
|
|
27
|
+
/// The session needs `NFCReaderUsageDescription` and the NFC Tag Reading
|
|
28
|
+
/// entitlement, which a free Personal Team can't sign, so the generator
|
|
29
|
+
/// writes them and keeps this file only in an app whose angular.json sets
|
|
30
|
+
/// `permissions.nfc`. LynxViewController registers the module by its
|
|
31
|
+
/// Objective-C name only when it's there, hence the `@objc` name.
|
|
32
|
+
@objc(AngularLynxNfcModule)
|
|
33
|
+
final class NfcModule: NSObject, LynxModule, NFCNDEFReaderSessionDelegate {
|
|
34
|
+
static var name: String { "AngularLynxNfc" }
|
|
35
|
+
|
|
36
|
+
static var methodLookup: [String: String] {
|
|
37
|
+
[
|
|
38
|
+
"state": NSStringFromSelector(#selector(state(_:))),
|
|
39
|
+
"start": NSStringFromSelector(#selector(start(_:callback:))),
|
|
40
|
+
"next": NSStringFromSelector(#selector(next(_:))),
|
|
41
|
+
"stop": NSStringFromSelector(#selector(stop(_:callback:))),
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// Every field below is read and written on this queue, which is also the
|
|
46
|
+
/// session's delegate queue: Lynx calls the module's methods on a thread
|
|
47
|
+
/// of its own.
|
|
48
|
+
private let queue = DispatchQueue(label: "dev.angularlynx.nfc")
|
|
49
|
+
|
|
50
|
+
private var session: NFCNDEFReaderSession?
|
|
51
|
+
/// `start`'s callback, until the sheet is up or the session fails.
|
|
52
|
+
private var starting: LynxCallbackBlock?
|
|
53
|
+
/// `next`'s callback, until a tag or the session's end answers it.
|
|
54
|
+
private var waiting: LynxCallbackBlock?
|
|
55
|
+
/// The tags found while nobody was waiting, oldest first.
|
|
56
|
+
private var answers: [[String: Any]] = []
|
|
57
|
+
/// Why the last session ended, for a `next` after it.
|
|
58
|
+
private var ended: [String: Any] = ["error": "stopped"]
|
|
59
|
+
private var multiple = false
|
|
60
|
+
private var message: NFCNDEFMessage?
|
|
61
|
+
private var alertMessage = ""
|
|
62
|
+
|
|
63
|
+
override init() {
|
|
64
|
+
super.init()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Swift can't import an optional initializer as optional, so Lynx's
|
|
68
|
+
// initWithParam: is required here. Registered without a param, the module
|
|
69
|
+
// ignores it.
|
|
70
|
+
init(param: Any) {
|
|
71
|
+
super.init()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@objc func state(_ callback: @escaping LynxCallbackBlock) {
|
|
75
|
+
callback(["state": NFCNDEFReaderSession.readingAvailable ? "ready" : "unsupported"])
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@objc func start(_ options: [String: Any], callback: @escaping LynxCallbackBlock) {
|
|
79
|
+
queue.async {
|
|
80
|
+
guard NFCNDEFReaderSession.readingAvailable else {
|
|
81
|
+
callback(["error": "unsupported", "message": "This iPhone can't read NFC tags."])
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
guard self.session == nil else {
|
|
85
|
+
callback(["error": "busy", "message": "Another NFC read or write is still open."])
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
self.message = nil
|
|
89
|
+
if let records = options["write"] as? [[String: Any]] {
|
|
90
|
+
guard let payloads = Self.payloads(records) else {
|
|
91
|
+
callback(["error": "failed", "message": "The records to write aren't valid NDEF records."])
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
self.message = NFCNDEFMessage(records: payloads)
|
|
95
|
+
}
|
|
96
|
+
self.multiple = options["multiple"] as? Bool ?? false
|
|
97
|
+
self.alertMessage = options["alertMessage"] as? String ?? "Hold your iPhone near the tag."
|
|
98
|
+
self.answers = []
|
|
99
|
+
// invalidateAfterFirstRead is ignored once the delegate takes tags
|
|
100
|
+
// (didDetect tags:), which it does to write and to say why a tag
|
|
101
|
+
// failed: the module ends the session itself.
|
|
102
|
+
let session = NFCNDEFReaderSession(delegate: self, queue: self.queue, invalidateAfterFirstRead: false)
|
|
103
|
+
session.alertMessage = self.alertMessage
|
|
104
|
+
self.session = session
|
|
105
|
+
self.starting = callback
|
|
106
|
+
session.begin()
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@objc func next(_ callback: @escaping LynxCallbackBlock) {
|
|
111
|
+
queue.async {
|
|
112
|
+
if !self.answers.isEmpty {
|
|
113
|
+
callback(self.answers.removeFirst())
|
|
114
|
+
} else if self.session == nil {
|
|
115
|
+
callback(self.ended)
|
|
116
|
+
} else if self.waiting != nil {
|
|
117
|
+
callback(["error": "busy", "message": "The module is already waiting for a tag."])
|
|
118
|
+
} else {
|
|
119
|
+
self.waiting = callback
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
@objc func stop(_ options: [String: Any], callback: @escaping LynxCallbackBlock) {
|
|
125
|
+
queue.async {
|
|
126
|
+
if let session = self.session {
|
|
127
|
+
// Forgotten first, so the delegate's call for this session, which
|
|
128
|
+
// comes later, finds it isn't the current one.
|
|
129
|
+
self.session = nil
|
|
130
|
+
if let errorMessage = options["errorMessage"] as? String {
|
|
131
|
+
session.invalidate(errorMessage: errorMessage)
|
|
132
|
+
} else {
|
|
133
|
+
if let message = options["message"] as? String {
|
|
134
|
+
session.alertMessage = message
|
|
135
|
+
}
|
|
136
|
+
session.invalidate()
|
|
137
|
+
}
|
|
138
|
+
self.end(["error": "stopped"])
|
|
139
|
+
}
|
|
140
|
+
callback([:])
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/// Answers whoever waits with why the session ended, and keeps it for a
|
|
145
|
+
/// later `next`.
|
|
146
|
+
private func end(_ result: [String: Any]) {
|
|
147
|
+
ended = result
|
|
148
|
+
if let starting {
|
|
149
|
+
self.starting = nil
|
|
150
|
+
starting(result)
|
|
151
|
+
}
|
|
152
|
+
if let waiting {
|
|
153
|
+
self.waiting = nil
|
|
154
|
+
waiting(result)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private func deliver(_ answer: [String: Any]) {
|
|
159
|
+
if let waiting {
|
|
160
|
+
self.waiting = nil
|
|
161
|
+
waiting(answer)
|
|
162
|
+
} else {
|
|
163
|
+
answers.append(answer)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// MARK: NFCNDEFReaderSessionDelegate
|
|
168
|
+
|
|
169
|
+
func readerSessionDidBecomeActive(_ session: NFCNDEFReaderSession) {
|
|
170
|
+
guard session === self.session, let starting else { return }
|
|
171
|
+
self.starting = nil
|
|
172
|
+
starting([:])
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
|
|
176
|
+
// A session the runtime stopped has already been answered.
|
|
177
|
+
guard session === self.session else { return }
|
|
178
|
+
self.session = nil
|
|
179
|
+
end(Self.endResult(error))
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/// Required, but iOS calls `didDetect tags:` instead once the delegate has
|
|
183
|
+
/// it.
|
|
184
|
+
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {}
|
|
185
|
+
|
|
186
|
+
func readerSession(_ session: NFCNDEFReaderSession, didDetect tags: [any NFCNDEFTag]) {
|
|
187
|
+
guard session === self.session else { return }
|
|
188
|
+
guard tags.count == 1, let tag = tags.first else {
|
|
189
|
+
session.alertMessage = "More than one tag is near the iPhone. Hold just one near it."
|
|
190
|
+
restartPolling(session)
|
|
191
|
+
return
|
|
192
|
+
}
|
|
193
|
+
session.connect(to: tag) { error in
|
|
194
|
+
self.queue.async {
|
|
195
|
+
guard session === self.session else { return }
|
|
196
|
+
if let error {
|
|
197
|
+
self.tagFailed(session, "failed", "The tag moved away before it could be read: \(error.localizedDescription)")
|
|
198
|
+
return
|
|
199
|
+
}
|
|
200
|
+
tag.queryNDEFStatus { status, capacity, error in
|
|
201
|
+
self.queue.async {
|
|
202
|
+
guard session === self.session else { return }
|
|
203
|
+
if let error {
|
|
204
|
+
self.tagFailed(session, "failed", "The tag couldn't be read: \(error.localizedDescription)")
|
|
205
|
+
} else if status == .notSupported {
|
|
206
|
+
self.tagFailed(session, "tagNotNdef", "This tag doesn't hold NDEF records.")
|
|
207
|
+
} else if let message = self.message {
|
|
208
|
+
self.write(message, to: tag, status: status, capacity: capacity, session: session)
|
|
209
|
+
} else {
|
|
210
|
+
self.read(tag, status: status, capacity: capacity, session: session)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private func read(
|
|
219
|
+
_ tag: any NFCNDEFTag, status: NFCNDEFStatus, capacity: Int, session: NFCNDEFReaderSession
|
|
220
|
+
) {
|
|
221
|
+
tag.readNDEF { message, error in
|
|
222
|
+
self.queue.async {
|
|
223
|
+
guard session === self.session else { return }
|
|
224
|
+
// An empty tag answers with this error rather than an empty message.
|
|
225
|
+
if let error = error as? NFCReaderError, error.code == .ndefReaderSessionErrorZeroLengthMessage {
|
|
226
|
+
self.tagRead(session, Self.tag([], writable: status == .readWrite, capacity: capacity))
|
|
227
|
+
} else if let message {
|
|
228
|
+
self.tagRead(session, Self.tag(message.records, writable: status == .readWrite, capacity: capacity))
|
|
229
|
+
} else {
|
|
230
|
+
self.tagFailed(
|
|
231
|
+
session, "failed", "The tag couldn't be read: \(error?.localizedDescription ?? "no message")")
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private func write(
|
|
238
|
+
_ message: NFCNDEFMessage, to tag: any NFCNDEFTag, status: NFCNDEFStatus, capacity: Int,
|
|
239
|
+
session: NFCNDEFReaderSession
|
|
240
|
+
) {
|
|
241
|
+
guard status == .readWrite else {
|
|
242
|
+
tagFailed(session, "readOnly", "This tag is locked, so it can't be written to.")
|
|
243
|
+
return
|
|
244
|
+
}
|
|
245
|
+
guard message.length <= capacity else {
|
|
246
|
+
tagFailed(session, "tooLarge", "The records take \(message.length) bytes, and this tag holds \(capacity).")
|
|
247
|
+
return
|
|
248
|
+
}
|
|
249
|
+
tag.writeNDEF(message) { error in
|
|
250
|
+
self.queue.async {
|
|
251
|
+
guard session === self.session else { return }
|
|
252
|
+
if let error {
|
|
253
|
+
self.tagFailed(session, "failed", "The tag couldn't be written: \(error.localizedDescription)")
|
|
254
|
+
} else {
|
|
255
|
+
self.tagRead(session, Self.tag(message.records, writable: true, capacity: capacity))
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/// A tag read or written: the runtime takes it, and a scan listens for the
|
|
262
|
+
/// next one.
|
|
263
|
+
private func tagRead(_ session: NFCNDEFReaderSession, _ tag: [String: Any]) {
|
|
264
|
+
deliver(["tag": tag])
|
|
265
|
+
if multiple {
|
|
266
|
+
session.alertMessage = alertMessage
|
|
267
|
+
restartPolling(session)
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/// A tag that couldn't be read or written. A scan says so on the sheet and
|
|
272
|
+
/// listens for the next; a single read or write rejects with it, and the
|
|
273
|
+
/// runtime closes the sheet with the message.
|
|
274
|
+
private func tagFailed(_ session: NFCNDEFReaderSession, _ code: String, _ message: String) {
|
|
275
|
+
if multiple {
|
|
276
|
+
session.alertMessage = message
|
|
277
|
+
restartPolling(session)
|
|
278
|
+
} else {
|
|
279
|
+
deliver(["error": code, "message": message])
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/// Listens again after a moment, so the tag just handled, or the ones that
|
|
284
|
+
/// were too many, can be taken away first.
|
|
285
|
+
private func restartPolling(_ session: NFCNDEFReaderSession) {
|
|
286
|
+
queue.asyncAfter(deadline: .now() + 0.5) {
|
|
287
|
+
guard session === self.session else { return }
|
|
288
|
+
session.restartPolling()
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// MARK: Converting
|
|
293
|
+
|
|
294
|
+
private static func endResult(_ error: Error) -> [String: Any] {
|
|
295
|
+
let message = error.localizedDescription
|
|
296
|
+
guard let error = error as? NFCReaderError else {
|
|
297
|
+
return ["error": "failed", "message": message]
|
|
298
|
+
}
|
|
299
|
+
switch error.code {
|
|
300
|
+
case .readerSessionInvalidationErrorUserCanceled:
|
|
301
|
+
return ["error": "canceled", "message": "The NFC session was canceled."]
|
|
302
|
+
case .readerSessionInvalidationErrorSessionTimeout:
|
|
303
|
+
return ["error": "timeout", "message": "No tag came near the iPhone in time."]
|
|
304
|
+
case .readerSessionInvalidationErrorFirstNDEFTagRead:
|
|
305
|
+
return ["error": "stopped"]
|
|
306
|
+
case .readerErrorUnsupportedFeature:
|
|
307
|
+
return ["error": "unsupported", "message": "This iPhone can't read NFC tags."]
|
|
308
|
+
case .readerSessionInvalidationErrorSystemIsBusy:
|
|
309
|
+
return ["error": "busy", "message": "iOS is busy with another NFC session."]
|
|
310
|
+
default:
|
|
311
|
+
return ["error": "failed", "message": "\(message) (\(error.code.rawValue))"]
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private static func tag(_ records: [NFCNDEFPayload], writable: Bool, capacity: Int) -> [String: Any] {
|
|
316
|
+
[
|
|
317
|
+
"records": records.map { record in
|
|
318
|
+
[
|
|
319
|
+
"tnf": Int(record.typeNameFormat.rawValue),
|
|
320
|
+
"type": record.type.base64EncodedString(),
|
|
321
|
+
"id": record.identifier.base64EncodedString(),
|
|
322
|
+
"payload": record.payload.base64EncodedString(),
|
|
323
|
+
] as [String: Any]
|
|
324
|
+
},
|
|
325
|
+
"writable": writable,
|
|
326
|
+
"capacity": capacity,
|
|
327
|
+
]
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/// The runtime's records as payloads, or nil for one that isn't valid.
|
|
331
|
+
private static func payloads(_ records: [[String: Any]]) -> [NFCNDEFPayload]? {
|
|
332
|
+
var payloads: [NFCNDEFPayload] = []
|
|
333
|
+
for record in records {
|
|
334
|
+
guard
|
|
335
|
+
let tnf = (record["tnf"] as? NSNumber).flatMap({ NFCTypeNameFormat(rawValue: $0.uint8Value) }),
|
|
336
|
+
let type = (record["type"] as? String).flatMap({ Data(base64Encoded: $0) }),
|
|
337
|
+
let payload = (record["payload"] as? String).flatMap({ Data(base64Encoded: $0) })
|
|
338
|
+
else {
|
|
339
|
+
return nil
|
|
340
|
+
}
|
|
341
|
+
let identifier = (record["id"] as? String).flatMap { Data(base64Encoded: $0) } ?? Data()
|
|
342
|
+
payloads.append(NFCNDEFPayload(format: tnf, type: type, identifier: identifier, payload: payload))
|
|
343
|
+
}
|
|
344
|
+
return payloads
|
|
345
|
+
}
|
|
346
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Lynx
|
|
3
|
+
import Photos
|
|
4
|
+
|
|
5
|
+
/// The native module behind the runtime's `LynxPhotoLibrary`, which calls it
|
|
6
|
+
/// as `NativeModules.AngularLynxPhotoLibrary` (from the background thread,
|
|
7
|
+
/// where NativeModules lives).
|
|
8
|
+
///
|
|
9
|
+
/// Only an app with `permissions.savePhotos` in angular.json has this file:
|
|
10
|
+
/// the generator deletes it from the others. LynxViewController registers it
|
|
11
|
+
/// by its Objective-C name, so the shell builds either way.
|
|
12
|
+
///
|
|
13
|
+
/// It asks for add-only access (`.addOnly`), the access the share sheet's
|
|
14
|
+
/// Save Image uses: the app can add photos and videos but not see the
|
|
15
|
+
/// library, so iOS asks with NSPhotoLibraryAddUsageDescription, and the app
|
|
16
|
+
/// needs no NSPhotoLibraryUsageDescription. A photo goes to Photos as its
|
|
17
|
+
/// bytes; a video, which can only be added from a file, is written to a
|
|
18
|
+
/// temporary file of its own name first, whose extension tells Photos the
|
|
19
|
+
/// format. Each method calls back once with
|
|
20
|
+
/// `{ permission }`, `{ id, permission }` or `{ error, message, permission }`,
|
|
21
|
+
/// where `error` is one of the runtime's PhotoLibraryErrorCode values.
|
|
22
|
+
@objc(AngularLynxPhotoLibraryModule)
|
|
23
|
+
final class PhotoLibraryModule: NSObject, LynxModule {
|
|
24
|
+
static var name: String { "AngularLynxPhotoLibrary" }
|
|
25
|
+
|
|
26
|
+
static var methodLookup: [String: String] {
|
|
27
|
+
[
|
|
28
|
+
"permission": NSStringFromSelector(#selector(permission(_:))),
|
|
29
|
+
"save": NSStringFromSelector(#selector(save(_:callback:))),
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
override init() {
|
|
34
|
+
super.init()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Swift can't import an optional initializer as optional, so Lynx's
|
|
38
|
+
// initWithParam: is required here. Registered without a param, the module
|
|
39
|
+
// ignores it.
|
|
40
|
+
init(param: Any) {
|
|
41
|
+
super.init()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// `{ permission }`: whether the app may add to the library, without asking.
|
|
45
|
+
@objc func permission(_ callback: @escaping LynxCallbackBlock) {
|
|
46
|
+
callback(["permission": Self.permissionName(PHPhotoLibrary.authorizationStatus(for: .addOnly))])
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/// Adds `request`'s file (`{ base64, name, type }`) to the library, asking
|
|
50
|
+
/// for add-only access first if the app hasn't yet, and calls back with
|
|
51
|
+
/// the new asset's local identifier.
|
|
52
|
+
@objc func save(_ request: [String: Any], callback: @escaping LynxCallbackBlock) {
|
|
53
|
+
guard let base64 = request["base64"] as? String,
|
|
54
|
+
let data = Data(base64Encoded: base64)
|
|
55
|
+
else {
|
|
56
|
+
callback(Self.failure("failed", "The file's bytes couldn't be read."))
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
let type = request["type"] as? String ?? ""
|
|
60
|
+
let isVideo = type.hasPrefix("video/")
|
|
61
|
+
let name = Self.fileName(request["name"] as? String, isVideo: isVideo)
|
|
62
|
+
// The prompt shows the first time only; after that iOS answers at once.
|
|
63
|
+
// Its handler runs on a queue of Photos' own, which is where the work
|
|
64
|
+
// carries on: writing a video can take a moment.
|
|
65
|
+
PHPhotoLibrary.requestAuthorization(for: .addOnly) { status in
|
|
66
|
+
let permission = Self.permissionName(status)
|
|
67
|
+
guard status == .authorized || status == .limited else {
|
|
68
|
+
callback([
|
|
69
|
+
"error": "permission-denied",
|
|
70
|
+
"message": "The app isn't allowed to add to the photo library. The person can allow it in Settings.",
|
|
71
|
+
"permission": permission,
|
|
72
|
+
])
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
// A photo goes to Photos as its bytes. A video can only be added from
|
|
76
|
+
// a file, which Photos copies: having it move the file instead hands
|
|
77
|
+
// the daemon a sandbox extension for it, and on GitHub's iOS 27.2
|
|
78
|
+
// Simulator an add that moved a file was taken and never answered.
|
|
79
|
+
var directory: URL?
|
|
80
|
+
if isVideo {
|
|
81
|
+
let folder = FileManager.default.temporaryDirectory
|
|
82
|
+
.appendingPathComponent("AngularLynxPhotoLibrary", isDirectory: true)
|
|
83
|
+
.appendingPathComponent(UUID().uuidString, isDirectory: true)
|
|
84
|
+
do {
|
|
85
|
+
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
|
|
86
|
+
try data.write(to: folder.appendingPathComponent(name))
|
|
87
|
+
} catch {
|
|
88
|
+
// Photos' completion, which removes the folder otherwise, never
|
|
89
|
+
// runs for a file that wasn't written.
|
|
90
|
+
try? FileManager.default.removeItem(at: folder)
|
|
91
|
+
callback(Self.failure("failed", "The file couldn't be written to save: \(error.localizedDescription)", permission))
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
directory = folder
|
|
95
|
+
}
|
|
96
|
+
var id: String?
|
|
97
|
+
PHPhotoLibrary.shared().performChanges({
|
|
98
|
+
let creation = PHAssetCreationRequest.forAsset()
|
|
99
|
+
let options = PHAssetResourceCreationOptions()
|
|
100
|
+
// The name Photos keeps, and shows when the photo is exported.
|
|
101
|
+
options.originalFilename = name
|
|
102
|
+
if let directory {
|
|
103
|
+
creation.addResource(
|
|
104
|
+
with: .video, fileURL: directory.appendingPathComponent(name), options: options)
|
|
105
|
+
} else {
|
|
106
|
+
creation.addResource(with: .photo, data: data, options: options)
|
|
107
|
+
}
|
|
108
|
+
id = creation.placeholderForCreatedAsset?.localIdentifier
|
|
109
|
+
}) { success, error in
|
|
110
|
+
if let directory { try? FileManager.default.removeItem(at: directory) }
|
|
111
|
+
if success, let id {
|
|
112
|
+
callback(["id": id, "permission": permission])
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
callback(Self.failure(Self.errorCode(error), Self.message(error), permission))
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/// A file name that keeps the app's extension, which is how Photos tells
|
|
121
|
+
/// the format, without anything that would reach outside the directory.
|
|
122
|
+
private static func fileName(_ given: String?, isVideo: Bool) -> String {
|
|
123
|
+
let base = (given ?? "").replacingOccurrences(of: "/", with: "_")
|
|
124
|
+
if !base.isEmpty && base != "." && base != ".." { return base }
|
|
125
|
+
return isVideo ? "Video.mov" : "Photo.jpg"
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private static func permissionName(_ status: PHAuthorizationStatus) -> String {
|
|
129
|
+
switch status {
|
|
130
|
+
case .authorized: return "granted"
|
|
131
|
+
case .limited: return "limited"
|
|
132
|
+
case .denied, .restricted: return "denied"
|
|
133
|
+
case .notDetermined: return "undetermined"
|
|
134
|
+
@unknown default: return "denied"
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/// Photos' errors as the runtime's codes: a format it can't read is the
|
|
139
|
+
/// file's fault, and access errors are the person's answer.
|
|
140
|
+
private static func errorCode(_ error: Error?) -> String {
|
|
141
|
+
guard let error = error as NSError?, error.domain == PHPhotosErrorDomain else {
|
|
142
|
+
return "failed"
|
|
143
|
+
}
|
|
144
|
+
switch error.code {
|
|
145
|
+
case PHPhotosError.Code.invalidResource.rawValue:
|
|
146
|
+
return "unsupported-type"
|
|
147
|
+
case PHPhotosError.Code.accessUserDenied.rawValue,
|
|
148
|
+
PHPhotosError.Code.accessRestricted.rawValue:
|
|
149
|
+
return "permission-denied"
|
|
150
|
+
default:
|
|
151
|
+
return "failed"
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private static func message(_ error: Error?) -> String {
|
|
156
|
+
if let error {
|
|
157
|
+
return "The file couldn't be saved to the photo library: \(error.localizedDescription)"
|
|
158
|
+
}
|
|
159
|
+
return "The file couldn't be saved to the photo library."
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private static func failure(_ code: String, _ message: String, _ permission: String? = nil)
|
|
163
|
+
-> [String: Any]
|
|
164
|
+
{
|
|
165
|
+
var result: [String: Any] = ["error": code, "message": message]
|
|
166
|
+
if let permission { result["permission"] = permission }
|
|
167
|
+
return result
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Lynx
|
|
3
|
+
|
|
4
|
+
/// The native module behind the runtime's `LynxShareTarget`, which calls it as
|
|
5
|
+
/// `NativeModules.AngularLynxShareTarget` (from the background thread, where
|
|
6
|
+
/// NativeModules lives).
|
|
7
|
+
///
|
|
8
|
+
/// iOS puts an app in other apps' share sheets only through a Share
|
|
9
|
+
/// Extension: a separate process, with a memory limit of about 120 MB, that
|
|
10
|
+
/// can't run Lynx. So the extension (ShareExtension/ShareViewController.swift)
|
|
11
|
+
/// only copies what the person shares into the App Group container both
|
|
12
|
+
/// processes can read, one folder per share, and opens the app with the
|
|
13
|
+
/// share scheme. This module hands those shares to the runtime and deletes
|
|
14
|
+
/// them: `take` calls back with `{ shares }`, each `{ title?, text?, url?,
|
|
15
|
+
/// files: [{ base64, name, type }] }`, oldest first, and `more` when some
|
|
16
|
+
/// are left for another take (maxBytesPerTake). A share waits in the
|
|
17
|
+
/// container until the app takes it, so one that arrives while the app
|
|
18
|
+
/// isn't running, or before Angular has started, isn't lost, and none is
|
|
19
|
+
/// handled twice. SceneDelegate sends the `angularLynxShare` global event
|
|
20
|
+
/// when the extension opens the app, and the runtime takes the shares then,
|
|
21
|
+
/// and once as it starts.
|
|
22
|
+
///
|
|
23
|
+
/// The generator leaves this file and the extension out of an app whose
|
|
24
|
+
/// angular.json has no `shareTarget`, and LynxViewController registers the
|
|
25
|
+
/// module by its Objective-C name only when it's there, hence the `@objc`
|
|
26
|
+
/// name.
|
|
27
|
+
@objc(AngularLynxShareTargetModule)
|
|
28
|
+
final class ShareTargetModule: NSObject, LynxModule {
|
|
29
|
+
static var name: String { "AngularLynxShareTarget" }
|
|
30
|
+
|
|
31
|
+
static var methodLookup: [String: String] {
|
|
32
|
+
["take": NSStringFromSelector(#selector(take(_:)))]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// The folder of the App Group container the extension writes each share
|
|
36
|
+
/// to, as `<sortable time>-<UUID>/share.json` beside its files. The
|
|
37
|
+
/// extension writes a share under a name starting with `.` and renames it
|
|
38
|
+
/// once it's complete, so a share being written is never taken half done.
|
|
39
|
+
static let folderName = "AngularLynxShares"
|
|
40
|
+
|
|
41
|
+
/// One `take` at a time: two would read the same folders.
|
|
42
|
+
private static let queue = DispatchQueue(label: "dev.angularlynx.share-target")
|
|
43
|
+
|
|
44
|
+
override init() {
|
|
45
|
+
super.init()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Swift can't import an optional initializer as optional, so Lynx's
|
|
49
|
+
// initWithParam: is required here. Registered without a param, the module
|
|
50
|
+
// ignores it.
|
|
51
|
+
init(param: Any) {
|
|
52
|
+
super.init()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
@objc func take(_ callback: @escaping LynxCallbackBlock) {
|
|
56
|
+
// Reading the files and encoding them is slow for a large photo, and
|
|
57
|
+
// Lynx calls modules on its own thread, which mustn't wait on the disk.
|
|
58
|
+
Self.queue.async {
|
|
59
|
+
let (shares, more) = Self.takeWaiting()
|
|
60
|
+
callback(["shares": shares, "more": more])
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// The most bytes of files one `take` hands back: 20 MiB, a share's own
|
|
65
|
+
/// limit. Shares wait in the App Group while the app isn't running, and
|
|
66
|
+
/// reading a backlog of large ones in one go would hold all of it as
|
|
67
|
+
/// base64 at once; `more` has the runtime take again for the rest.
|
|
68
|
+
private static let maxBytesPerTake = 20 * 1024 * 1024
|
|
69
|
+
|
|
70
|
+
/// The shares already handed to the runtime in this process. Each is
|
|
71
|
+
/// deleted once read, but one that can't be deleted mustn't come back on
|
|
72
|
+
/// every take, or a `more` drain would hand over the same share forever.
|
|
73
|
+
/// Only `queue` touches it.
|
|
74
|
+
private static var taken = Set<String>()
|
|
75
|
+
|
|
76
|
+
/// The App Group container's share folder, or nil when the app has no App
|
|
77
|
+
/// Group: Info.plist's `AngularLynxShareGroup` names it, which the
|
|
78
|
+
/// generator writes with the entitlement.
|
|
79
|
+
static func folder() -> URL? {
|
|
80
|
+
guard let group = Bundle.main.object(forInfoDictionaryKey: "AngularLynxShareGroup") as? String,
|
|
81
|
+
let container = FileManager.default.containerURL(
|
|
82
|
+
forSecurityApplicationGroupIdentifier: group)
|
|
83
|
+
else { return nil }
|
|
84
|
+
return container.appendingPathComponent(folderName, isDirectory: true)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/// The oldest waiting shares, up to maxBytesPerTake of files (always at
|
|
88
|
+
/// least one), and whether more are waiting.
|
|
89
|
+
private static func takeWaiting() -> ([[String: Any]], Bool) {
|
|
90
|
+
guard let folder = folder(),
|
|
91
|
+
let names = try? FileManager.default.contentsOfDirectory(atPath: folder.path)
|
|
92
|
+
else { return ([], false) }
|
|
93
|
+
let waiting = names.sorted().filter { !$0.hasPrefix(".") && !taken.contains($0) }
|
|
94
|
+
var shares: [[String: Any]] = []
|
|
95
|
+
var bytes = 0
|
|
96
|
+
for name in waiting {
|
|
97
|
+
let share = folder.appendingPathComponent(name, isDirectory: true)
|
|
98
|
+
let size = folderSize(share)
|
|
99
|
+
if !shares.isEmpty, bytes + size > maxBytesPerTake {
|
|
100
|
+
return (shares, true)
|
|
101
|
+
}
|
|
102
|
+
bytes += size
|
|
103
|
+
taken.insert(name)
|
|
104
|
+
if let read = read(share) {
|
|
105
|
+
shares.append(read)
|
|
106
|
+
}
|
|
107
|
+
// Taken, or unreadable: either way it mustn't come back on every take.
|
|
108
|
+
try? FileManager.default.removeItem(at: share)
|
|
109
|
+
}
|
|
110
|
+
return (shares, false)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/// The bytes of a share's files on disk.
|
|
114
|
+
private static func folderSize(_ share: URL) -> Int {
|
|
115
|
+
let files =
|
|
116
|
+
(try? FileManager.default.contentsOfDirectory(
|
|
117
|
+
at: share, includingPropertiesForKeys: [.fileSizeKey])) ?? []
|
|
118
|
+
return files.reduce(0) { total, file in
|
|
119
|
+
total + ((try? file.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/// A share's `share.json`, with each file it lists read in as base64, the
|
|
124
|
+
/// way the runtime's LynxFilePicker receives files.
|
|
125
|
+
private static func read(_ share: URL) -> [String: Any]? {
|
|
126
|
+
guard
|
|
127
|
+
let data = try? Data(contentsOf: share.appendingPathComponent("share.json")),
|
|
128
|
+
let manifest = try? JSONSerialization.jsonObject(with: data) as? [String: Any]
|
|
129
|
+
else { return nil }
|
|
130
|
+
var result: [String: Any] = [:]
|
|
131
|
+
for key in ["title", "text", "url"] {
|
|
132
|
+
if let value = manifest[key] as? String {
|
|
133
|
+
result[key] = value
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
var files: [[String: Any]] = []
|
|
137
|
+
for file in manifest["files"] as? [[String: Any]] ?? [] {
|
|
138
|
+
guard let path = file["path"] as? String,
|
|
139
|
+
let bytes = try? Data(contentsOf: share.appendingPathComponent(path))
|
|
140
|
+
else { continue }
|
|
141
|
+
files.append([
|
|
142
|
+
"base64": bytes.base64EncodedString(),
|
|
143
|
+
"name": file["name"] as? String ?? path,
|
|
144
|
+
"type": file["type"] as? String ?? "application/octet-stream",
|
|
145
|
+
])
|
|
146
|
+
}
|
|
147
|
+
result["files"] = files
|
|
148
|
+
return result
|
|
149
|
+
}
|
|
150
|
+
}
|