@blotch/angular-lynx-native 22.3.1 → 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 +140 -1
- 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 +35 -2
- package/templates/android/angular-lynx.properties +2 -0
- package/templates/android/app/build.gradle.kts +51 -2
- package/templates/android/app/src/main/AndroidManifest.xml +14 -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,387 @@
|
|
|
1
|
+
import UIKit
|
|
2
|
+
import UniformTypeIdentifiers
|
|
3
|
+
|
|
4
|
+
/// The app's Share Extension: what iOS runs when the person picks the app in
|
|
5
|
+
/// another app's share sheet. The generator adds it to an app whose
|
|
6
|
+
/// angular.json has `shareTarget`, and its Info.plist's activation rule
|
|
7
|
+
/// lists the kinds of things `shareTarget.mimeTypes` names.
|
|
8
|
+
///
|
|
9
|
+
/// An extension is a process of its own, which iOS ends past about 120 MB,
|
|
10
|
+
/// so it can't run Lynx or the Angular app. It copies what's shared into
|
|
11
|
+
/// the App Group container it shares with the app, under
|
|
12
|
+
/// `AngularLynxShares/`, opens the app with the share scheme, and closes: the
|
|
13
|
+
/// app's ShareTargetModule hands the share to the runtime's
|
|
14
|
+
/// `LynxShareTarget`. Files are copied from the file iOS hands over, never
|
|
15
|
+
/// read into memory, so a long video doesn't come near the limit.
|
|
16
|
+
///
|
|
17
|
+
/// What it shows is minimal: a card saying it's sending to the app while it
|
|
18
|
+
/// copies, which most shares never see for long.
|
|
19
|
+
final class ShareViewController: UIViewController {
|
|
20
|
+
/// The most bytes of files one share hands the app, all its files
|
|
21
|
+
/// together: 20 MiB, as LynxFilePicker's FILE_PICKER_MAX_BYTES. The app
|
|
22
|
+
/// reads them into memory as base64 to pass them to JavaScript, so a file
|
|
23
|
+
/// that would take it past that is left out.
|
|
24
|
+
private static let maxBytes = 20 * 1024 * 1024
|
|
25
|
+
|
|
26
|
+
/// The types `shareTarget.mimeTypes` lists, from the extension's
|
|
27
|
+
/// Info.plist: what's shared is checked against them, since the
|
|
28
|
+
/// activation rule can only name broad kinds, such as "files".
|
|
29
|
+
private let mimeTypes =
|
|
30
|
+
Bundle.main.object(forInfoDictionaryKey: "AngularLynxShareMimeTypes") as? [String] ?? []
|
|
31
|
+
|
|
32
|
+
/// The attachments are read on their own queues; their results are put
|
|
33
|
+
/// together on this one.
|
|
34
|
+
private let queue = DispatchQueue(label: "dev.angularlynx.share-extension")
|
|
35
|
+
|
|
36
|
+
private var sharedTitle: String?
|
|
37
|
+
private var texts: [String] = []
|
|
38
|
+
/// The items (by index) whose attachments gave text, whose content text
|
|
39
|
+
/// is then left out (see receive()).
|
|
40
|
+
private var itemsWithText = Set<Int>()
|
|
41
|
+
/// The item (by index) whose attachment gave the share's url: its content
|
|
42
|
+
/// text is the page's title, as Safari gives it.
|
|
43
|
+
private var urlItem: Int?
|
|
44
|
+
private var url: String?
|
|
45
|
+
private var files: [[String: String]] = []
|
|
46
|
+
private var bytes = 0
|
|
47
|
+
private var started = false
|
|
48
|
+
private let spinner = UIActivityIndicatorView(style: .medium)
|
|
49
|
+
private let label = UILabel()
|
|
50
|
+
|
|
51
|
+
override func viewDidLoad() {
|
|
52
|
+
super.viewDidLoad()
|
|
53
|
+
view.backgroundColor = UIColor.black.withAlphaComponent(0.2)
|
|
54
|
+
let card = UIView()
|
|
55
|
+
card.backgroundColor = .secondarySystemBackground
|
|
56
|
+
card.layer.cornerRadius = 16
|
|
57
|
+
card.translatesAutoresizingMaskIntoConstraints = false
|
|
58
|
+
spinner.startAnimating()
|
|
59
|
+
label.numberOfLines = 0
|
|
60
|
+
let appName =
|
|
61
|
+
Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? "the app"
|
|
62
|
+
label.text = "Sending to \(appName)…"
|
|
63
|
+
label.font = .preferredFont(forTextStyle: .body)
|
|
64
|
+
label.adjustsFontForContentSizeCategory = true
|
|
65
|
+
let stack = UIStackView(arrangedSubviews: [spinner, label])
|
|
66
|
+
stack.axis = .horizontal
|
|
67
|
+
stack.spacing = 12
|
|
68
|
+
stack.translatesAutoresizingMaskIntoConstraints = false
|
|
69
|
+
card.addSubview(stack)
|
|
70
|
+
view.addSubview(card)
|
|
71
|
+
NSLayoutConstraint.activate([
|
|
72
|
+
card.centerXAnchor.constraint(equalTo: view.centerXAnchor),
|
|
73
|
+
card.centerYAnchor.constraint(equalTo: view.centerYAnchor),
|
|
74
|
+
card.widthAnchor.constraint(lessThanOrEqualTo: view.widthAnchor, constant: -32),
|
|
75
|
+
stack.topAnchor.constraint(equalTo: card.topAnchor, constant: 20),
|
|
76
|
+
stack.bottomAnchor.constraint(equalTo: card.bottomAnchor, constant: -20),
|
|
77
|
+
stack.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 24),
|
|
78
|
+
stack.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -24),
|
|
79
|
+
])
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
override func viewDidAppear(_ animated: Bool) {
|
|
83
|
+
super.viewDidAppear(animated)
|
|
84
|
+
guard !started else { return }
|
|
85
|
+
started = true
|
|
86
|
+
receive()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// MARK: Reading the share
|
|
90
|
+
|
|
91
|
+
private func receive() {
|
|
92
|
+
guard let folder = Self.sharesFolder() else {
|
|
93
|
+
// Without the App Group there's nowhere the app can read: the
|
|
94
|
+
// entitlement is missing, as when the app was signed without it.
|
|
95
|
+
finish(opening: false)
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
let id = "\(Self.sortableNow())-\(UUID().uuidString)"
|
|
99
|
+
// Written under a hidden name, then renamed once complete, so the app
|
|
100
|
+
// never takes a share that's half written.
|
|
101
|
+
let pending = folder.appendingPathComponent(".\(id)", isDirectory: true)
|
|
102
|
+
do {
|
|
103
|
+
try FileManager.default.createDirectory(at: pending, withIntermediateDirectories: true)
|
|
104
|
+
} catch {
|
|
105
|
+
finish(opening: false)
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
let items = extensionContext?.inputItems.compactMap { $0 as? NSExtensionItem } ?? []
|
|
110
|
+
let group = DispatchGroup()
|
|
111
|
+
for (index, item) in items.enumerated() {
|
|
112
|
+
if sharedTitle == nil, let title = item.attributedTitle?.string, !title.isEmpty {
|
|
113
|
+
sharedTitle = title
|
|
114
|
+
}
|
|
115
|
+
for provider in item.attachments ?? [] {
|
|
116
|
+
read(provider, of: index, into: pending, group: group)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
group.notify(queue: queue) {
|
|
120
|
+
// A share sheet that hands over text only as the item's content: the
|
|
121
|
+
// page's title beside the link of the item that gave the url, as
|
|
122
|
+
// Safari's, or else a selection's text, when the item's attachments
|
|
123
|
+
// gave the app none.
|
|
124
|
+
for (index, item) in items.enumerated() {
|
|
125
|
+
guard let content = item.attributedContentText?.string, !content.isEmpty else { continue }
|
|
126
|
+
if index == self.urlItem {
|
|
127
|
+
if self.sharedTitle == nil { self.sharedTitle = content }
|
|
128
|
+
} else if !self.itemsWithText.contains(index), self.accepts("text/plain") {
|
|
129
|
+
self.texts.append(content)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
let complete = self.write(pending, as: folder.appendingPathComponent(id, isDirectory: true))
|
|
133
|
+
DispatchQueue.main.async { self.finish(opening: complete) }
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/// Reads one attachment, as the first of these the app takes: a link
|
|
138
|
+
/// (`text/uri-list`), text (`text/plain`), the link as text for an app
|
|
139
|
+
/// that takes text but not links (as Android shares a link), or a file of
|
|
140
|
+
/// an accepted type. An attachment often offers several of them, such as
|
|
141
|
+
/// a page's link and its text, so one the app doesn't take falls through
|
|
142
|
+
/// to the next rather than ending the attachment.
|
|
143
|
+
private func read(
|
|
144
|
+
_ provider: NSItemProvider, of itemIndex: Int, into folder: URL, group: DispatchGroup
|
|
145
|
+
) {
|
|
146
|
+
/// Called on the queue.
|
|
147
|
+
let addText = { (text: String) in
|
|
148
|
+
self.texts.append(text)
|
|
149
|
+
self.itemsWithText.insert(itemIndex)
|
|
150
|
+
}
|
|
151
|
+
let isFileURL = provider.hasItemConformingToTypeIdentifier(UTType.fileURL.identifier)
|
|
152
|
+
let isLink = !isFileURL && provider.hasItemConformingToTypeIdentifier(UTType.url.identifier)
|
|
153
|
+
let isText = !isFileURL && provider.hasItemConformingToTypeIdentifier(UTType.plainText.identifier)
|
|
154
|
+
|
|
155
|
+
if isLink, accepts("text/uri-list") {
|
|
156
|
+
readLink(provider, group: group) { link in
|
|
157
|
+
// The first link is the share's url, as the Web Share API's
|
|
158
|
+
// ShareData has one; any more go with the text.
|
|
159
|
+
if self.url == nil {
|
|
160
|
+
self.url = link
|
|
161
|
+
self.urlItem = itemIndex
|
|
162
|
+
} else {
|
|
163
|
+
addText(link)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
if isText, accepts("text/plain") {
|
|
169
|
+
group.enter()
|
|
170
|
+
provider.loadItem(forTypeIdentifier: UTType.plainText.identifier) { item, _ in
|
|
171
|
+
let text = (item as? String) ?? (item as? Data).flatMap { String(data: $0, encoding: .utf8) }
|
|
172
|
+
self.queue.async {
|
|
173
|
+
if let text, !text.isEmpty { addText(text) }
|
|
174
|
+
group.leave()
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return
|
|
178
|
+
}
|
|
179
|
+
if isLink, accepts("text/plain") {
|
|
180
|
+
readLink(provider, group: group) { link in addText(link) }
|
|
181
|
+
return
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// A file, in the first of its types that's content (not a link to it)
|
|
185
|
+
// and accepted. A provider lists its types from the richest down, such
|
|
186
|
+
// as public.heic before public.jpeg, which is the one to keep.
|
|
187
|
+
let identifier = provider.registeredTypeIdentifiers.first { identifier in
|
|
188
|
+
guard let type = UTType(identifier), type.conforms(to: .data), !type.conforms(to: .url)
|
|
189
|
+
else { return false }
|
|
190
|
+
return accepts(type.preferredMIMEType ?? "application/octet-stream")
|
|
191
|
+
}
|
|
192
|
+
if let identifier {
|
|
193
|
+
let type = UTType(identifier)
|
|
194
|
+
group.enter()
|
|
195
|
+
_ = provider.loadFileRepresentation(forTypeIdentifier: identifier) { file, _ in
|
|
196
|
+
// The file iOS hands over is deleted once this returns, so it's
|
|
197
|
+
// copied before then.
|
|
198
|
+
let copied = file.flatMap {
|
|
199
|
+
self.copy($0, named: Self.fileName($0, suggested: provider.suggestedName, type: type),
|
|
200
|
+
type: type, into: folder)
|
|
201
|
+
}
|
|
202
|
+
self.queue.async {
|
|
203
|
+
if let copied { self.files.append(copied) }
|
|
204
|
+
group.leave()
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// A file offered only as a link to it, as some file providers share:
|
|
211
|
+
// its type comes from its extension.
|
|
212
|
+
if isFileURL {
|
|
213
|
+
group.enter()
|
|
214
|
+
provider.loadItem(forTypeIdentifier: UTType.fileURL.identifier) { item, _ in
|
|
215
|
+
var copied: [String: String]?
|
|
216
|
+
if let file = (item as? URL) ?? (item as? Data).flatMap({ URL(dataRepresentation: $0, relativeTo: nil) }),
|
|
217
|
+
file.isFileURL
|
|
218
|
+
{
|
|
219
|
+
let type = UTType(filenameExtension: file.pathExtension)
|
|
220
|
+
if self.accepts(type?.preferredMIMEType ?? "application/octet-stream") {
|
|
221
|
+
let scoped = file.startAccessingSecurityScopedResource()
|
|
222
|
+
copied = self.copy(file, named: file.lastPathComponent, type: type, into: folder)
|
|
223
|
+
if scoped { file.stopAccessingSecurityScopedResource() }
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
self.queue.async {
|
|
227
|
+
if let copied { self.files.append(copied) }
|
|
228
|
+
group.leave()
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/// Loads an attachment's web link and hands it to `use` on the queue.
|
|
235
|
+
private func readLink(
|
|
236
|
+
_ provider: NSItemProvider, group: DispatchGroup, _ use: @escaping (String) -> Void
|
|
237
|
+
) {
|
|
238
|
+
group.enter()
|
|
239
|
+
provider.loadItem(forTypeIdentifier: UTType.url.identifier) { item, _ in
|
|
240
|
+
let link = (item as? URL) ?? (item as? Data).flatMap { URL(dataRepresentation: $0, relativeTo: nil) }
|
|
241
|
+
self.queue.async {
|
|
242
|
+
if let link, !link.isFileURL { use(link.absoluteString) }
|
|
243
|
+
group.leave()
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/// Copies `file` into the share, if it fits in what's left of the limit,
|
|
249
|
+
/// and returns its entry for share.json.
|
|
250
|
+
private func copy(_ file: URL, named name: String, type: UTType?, into folder: URL)
|
|
251
|
+
-> [String: String]?
|
|
252
|
+
{
|
|
253
|
+
let size = (try? file.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0
|
|
254
|
+
let path = "\(UUID().uuidString)-\(name)"
|
|
255
|
+
// Reserved under the queue, so two files can't both take the last of
|
|
256
|
+
// the limit.
|
|
257
|
+
let fits = queue.sync { () -> Bool in
|
|
258
|
+
guard bytes + size <= Self.maxBytes else { return false }
|
|
259
|
+
bytes += size
|
|
260
|
+
return true
|
|
261
|
+
}
|
|
262
|
+
guard fits else { return nil }
|
|
263
|
+
guard (try? FileManager.default.copyItem(at: file, to: folder.appendingPathComponent(path))) != nil
|
|
264
|
+
else {
|
|
265
|
+
queue.sync { bytes -= size }
|
|
266
|
+
return nil
|
|
267
|
+
}
|
|
268
|
+
return [
|
|
269
|
+
"path": path,
|
|
270
|
+
"name": name,
|
|
271
|
+
"type": type?.preferredMIMEType ?? "application/octet-stream",
|
|
272
|
+
]
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/// The name the app sees: the one the sharing app suggests, with the
|
|
276
|
+
/// type's extension when it has none, or the file's own.
|
|
277
|
+
private static func fileName(_ file: URL, suggested: String?, type: UTType?) -> String {
|
|
278
|
+
guard let suggested, !suggested.isEmpty else { return file.lastPathComponent }
|
|
279
|
+
if (suggested as NSString).pathExtension.isEmpty, let ext = type?.preferredFilenameExtension {
|
|
280
|
+
return "\(suggested).\(ext)"
|
|
281
|
+
}
|
|
282
|
+
return suggested
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/// Writes the share's share.json and moves the share where the app looks.
|
|
286
|
+
/// Returns false, leaving nothing behind, when there's nothing to hand
|
|
287
|
+
/// over: nothing shared was of a type the app takes.
|
|
288
|
+
private func write(_ pending: URL, as final: URL) -> Bool {
|
|
289
|
+
var manifest: [String: Any] = ["files": files]
|
|
290
|
+
if let sharedTitle { manifest["title"] = sharedTitle }
|
|
291
|
+
if !texts.isEmpty { manifest["text"] = texts.joined(separator: "\n") }
|
|
292
|
+
if let url { manifest["url"] = url }
|
|
293
|
+
guard files.count > 0 || !texts.isEmpty || url != nil,
|
|
294
|
+
let data = try? JSONSerialization.data(withJSONObject: manifest),
|
|
295
|
+
(try? data.write(to: pending.appendingPathComponent("share.json"))) != nil,
|
|
296
|
+
(try? FileManager.default.moveItem(at: pending, to: final)) != nil
|
|
297
|
+
else {
|
|
298
|
+
try? FileManager.default.removeItem(at: pending)
|
|
299
|
+
return false
|
|
300
|
+
}
|
|
301
|
+
return true
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// MARK: Handing it to the app
|
|
305
|
+
|
|
306
|
+
/// Opens the app, which takes the share, and closes the extension. If
|
|
307
|
+
/// the app doesn't open, the card says to open it, since the share waits
|
|
308
|
+
/// for it.
|
|
309
|
+
private func finish(opening: Bool) {
|
|
310
|
+
guard opening, let scheme = Bundle.main.object(forInfoDictionaryKey: "AngularLynxShareScheme") as? String,
|
|
311
|
+
let url = URL(string: "\(scheme)://share")
|
|
312
|
+
else {
|
|
313
|
+
extensionContext?.completeRequest(returningItems: nil)
|
|
314
|
+
return
|
|
315
|
+
}
|
|
316
|
+
openApp(url) { opened in
|
|
317
|
+
DispatchQueue.main.async {
|
|
318
|
+
guard !opened else {
|
|
319
|
+
self.extensionContext?.completeRequest(returningItems: nil)
|
|
320
|
+
return
|
|
321
|
+
}
|
|
322
|
+
let appName =
|
|
323
|
+
Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? "the app"
|
|
324
|
+
self.label.text = "Saved. Open \(appName) to see it."
|
|
325
|
+
self.spinner.stopAnimating()
|
|
326
|
+
self.spinner.isHidden = true
|
|
327
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
|
|
328
|
+
self.extensionContext?.completeRequest(returningItems: nil)
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/// A Share Extension has no API to open its app: `extensionContext.open`
|
|
335
|
+
/// works only in widgets, and `UIApplication.shared` isn't available to an
|
|
336
|
+
/// extension. The UIApplication is in the view controller's responder
|
|
337
|
+
/// chain, though, and its `open(_:options:completionHandler:)` opens the
|
|
338
|
+
/// app as a link would, so it's called through the Objective-C runtime,
|
|
339
|
+
/// as share extensions of apps on the App Store do. Apple doesn't support
|
|
340
|
+
/// it, so it's only a shortcut: the share waits in the App Group either
|
|
341
|
+
/// way, and the app takes it the next time it comes to the front.
|
|
342
|
+
/// `done` hears whether the app opened.
|
|
343
|
+
private func openApp(_ url: URL, done: @escaping (Bool) -> Void) {
|
|
344
|
+
let selector = NSSelectorFromString("openURL:options:completionHandler:")
|
|
345
|
+
var responder: UIResponder? = self
|
|
346
|
+
while let current = responder {
|
|
347
|
+
if current is UIApplication, current.responds(to: selector),
|
|
348
|
+
let implementation = current.method(for: selector)
|
|
349
|
+
{
|
|
350
|
+
typealias Open = @convention(c) (
|
|
351
|
+
AnyObject, Selector, NSURL, NSDictionary, (@convention(block) (Bool) -> Void)?
|
|
352
|
+
) -> Void
|
|
353
|
+
let open = unsafeBitCast(implementation, to: Open.self)
|
|
354
|
+
open(current, selector, url as NSURL, NSDictionary(), { opened in done(opened) })
|
|
355
|
+
return
|
|
356
|
+
}
|
|
357
|
+
responder = current.next
|
|
358
|
+
}
|
|
359
|
+
done(false)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// MARK: Helpers
|
|
363
|
+
|
|
364
|
+
/// Whether `shareTarget.mimeTypes` takes `mime`: the type itself, its
|
|
365
|
+
/// `type/*`, or `*/*`.
|
|
366
|
+
private func accepts(_ mime: String) -> Bool {
|
|
367
|
+
let mime = mime.lowercased()
|
|
368
|
+
let major = mime.split(separator: "/").first.map(String.init) ?? ""
|
|
369
|
+
return mimeTypes.contains { accepted in
|
|
370
|
+
accepted == "*/*" || accepted == mime || accepted == "\(major)/*"
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/// The App Group container's share folder (see ShareTargetModule).
|
|
375
|
+
private static func sharesFolder() -> URL? {
|
|
376
|
+
guard let group = Bundle.main.object(forInfoDictionaryKey: "AngularLynxShareGroup") as? String,
|
|
377
|
+
let container = FileManager.default.containerURL(
|
|
378
|
+
forSecurityApplicationGroupIdentifier: group)
|
|
379
|
+
else { return nil }
|
|
380
|
+
return container.appendingPathComponent("AngularLynxShares", isDirectory: true)
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/// Milliseconds since 1970, zero-padded, so the folders sort oldest first.
|
|
384
|
+
private static func sortableNow() -> String {
|
|
385
|
+
String(format: "%015.0f", Date().timeIntervalSince1970 * 1000)
|
|
386
|
+
}
|
|
387
|
+
}
|