@craft-native/ios 0.0.100 → 0.0.103
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 +37 -0
- package/dist/cli.js +2 -1
- package/dist/index.js +2 -1
- package/package.json +1 -1
- package/templates/CraftApp.swift +97 -4
package/README.md
CHANGED
|
@@ -143,6 +143,34 @@ Edit `craft.config.json` in your iOS project:
|
|
|
143
143
|
}
|
|
144
144
|
```
|
|
145
145
|
|
|
146
|
+
The app name is shown as written (`"HQ.training"` is fine); the Xcode
|
|
147
|
+
project, target and Swift types use a PascalCase identifier made from it
|
|
148
|
+
(`HQTraining`). A key left out of the file keeps its default.
|
|
149
|
+
|
|
150
|
+
Appearance and navigation:
|
|
151
|
+
|
|
152
|
+
```json
|
|
153
|
+
{
|
|
154
|
+
"appearance": "system",
|
|
155
|
+
"backgroundColor": "#f8fafc",
|
|
156
|
+
"backgroundColorDark": "#020617",
|
|
157
|
+
"swipeNavigation": true
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
- `appearance`: `"system"` follows the phone's Light/Dark setting (and the
|
|
162
|
+
page's `prefers-color-scheme`), with a status bar that reads on either;
|
|
163
|
+
`"light"` and `"dark"` pin one. Without it, `darkMode` decides as before.
|
|
164
|
+
- `backgroundColorDark`: the launch screen and webview background in Dark Mode.
|
|
165
|
+
- `swipeNavigation`: an edge swipe goes back and forward through the page's
|
|
166
|
+
history, pushState entries included.
|
|
167
|
+
|
|
168
|
+
Debug builds let Safari's Develop menu inspect the webview, and relay the
|
|
169
|
+
page's console, uncaught errors, rejected promises and failed script loads to
|
|
170
|
+
the device log as `[craft page] …`, so a page that only breaks inside the app
|
|
171
|
+
can be read with `xcrun simctl spawn booted log show --predicate 'process ==
|
|
172
|
+
"YourApp"'`. Release builds do neither.
|
|
173
|
+
|
|
146
174
|
When push notifications are enabled, generated Debug builds use the APNs
|
|
147
175
|
development environment and Release builds use the production environment.
|
|
148
176
|
The generated entitlements select the correct value from the active Xcode
|
|
@@ -240,6 +268,15 @@ console.log(nfcData); // Tag content
|
|
|
240
268
|
await window.craft.requestHealthAuthorization(['stepCount', 'heartRate']);
|
|
241
269
|
const steps = await window.craft.getHealthData('stepCount', startDate, endDate);
|
|
242
270
|
|
|
271
|
+
// Through craft-native/mobile's `health` service:
|
|
272
|
+
import { health } from 'craft-native/mobile'
|
|
273
|
+
// Read only: the sheet asks for access, not "access and update".
|
|
274
|
+
await health.requestAuthorization(['workouts', 'heartRate', 'sleep'], { write: false })
|
|
275
|
+
// Apple Health workouts (the watch's and every app's), newest first.
|
|
276
|
+
const workouts = await health.getWorkouts({ startDate: Date.now() - 30 * 86400000 })
|
|
277
|
+
// One value per local day: sums, averages, the latest weight, hours asleep.
|
|
278
|
+
const hrv = await health.getDailyStatistics('heartRateVariability', { startDate: Date.now() - 30 * 86400000 })
|
|
279
|
+
|
|
243
280
|
// Screen Capture
|
|
244
281
|
const screenshotBase64 = await window.craft.takeScreenshot();
|
|
245
282
|
|
package/dist/cli.js
CHANGED
|
@@ -390,12 +390,13 @@ async function init(options) {
|
|
|
390
390
|
}
|
|
391
391
|
const finalBundleId = bundleId || `com.craft.${name.toLowerCase()}`;
|
|
392
392
|
const bundleIdPrefix = finalBundleId.split(".").slice(0, -1).join(".");
|
|
393
|
+
const given = Object.fromEntries(Object.entries(options.config ?? {}).filter(([, value]) => value !== undefined));
|
|
393
394
|
const config = {
|
|
394
395
|
...DEFAULT_CONFIG,
|
|
395
396
|
appName: displayName,
|
|
396
397
|
bundleId: finalBundleId,
|
|
397
398
|
teamId: teamId || "",
|
|
398
|
-
...
|
|
399
|
+
...given
|
|
399
400
|
};
|
|
400
401
|
if (config.enableBackgroundLocation)
|
|
401
402
|
config.enableGeolocation = true;
|
package/dist/index.js
CHANGED
|
@@ -385,12 +385,13 @@ async function init(options) {
|
|
|
385
385
|
}
|
|
386
386
|
const finalBundleId = bundleId || `com.craft.${name.toLowerCase()}`;
|
|
387
387
|
const bundleIdPrefix = finalBundleId.split(".").slice(0, -1).join(".");
|
|
388
|
+
const given = Object.fromEntries(Object.entries(options.config ?? {}).filter(([, value]) => value !== undefined));
|
|
388
389
|
const config = {
|
|
389
390
|
...DEFAULT_CONFIG,
|
|
390
391
|
appName: displayName,
|
|
391
392
|
bundleId: finalBundleId,
|
|
392
393
|
teamId: teamId || "",
|
|
393
|
-
...
|
|
394
|
+
...given
|
|
394
395
|
};
|
|
395
396
|
if (config.enableBackgroundLocation)
|
|
396
397
|
config.enableGeolocation = true;
|
package/package.json
CHANGED
package/templates/CraftApp.swift
CHANGED
|
@@ -337,17 +337,40 @@ class AppState: ObservableObject {
|
|
|
337
337
|
@Published var config: CraftConfig
|
|
338
338
|
|
|
339
339
|
init() {
|
|
340
|
-
// Load config from craft.config.json if available
|
|
341
340
|
if let configURL = Bundle.main.url(forResource: "craft.config", withExtension: "json"),
|
|
342
|
-
let data = try? Data(contentsOf: configURL)
|
|
343
|
-
|
|
344
|
-
self.config = config
|
|
341
|
+
let data = try? Data(contentsOf: configURL) {
|
|
342
|
+
self.config = CraftConfig.load(from: data)
|
|
345
343
|
} else {
|
|
346
344
|
self.config = CraftConfig()
|
|
347
345
|
}
|
|
348
346
|
}
|
|
349
347
|
}
|
|
350
348
|
|
|
349
|
+
extension CraftConfig {
|
|
350
|
+
/// The bundled config laid over the defaults, key by key.
|
|
351
|
+
///
|
|
352
|
+
/// Decoded whole, one missing or mistyped key failed the entire file and
|
|
353
|
+
/// the app ran on `CraftConfig()` — every capability off, silently: the
|
|
354
|
+
/// network bridge refused, the page read that as offline, and nothing said
|
|
355
|
+
/// why. A key the file leaves out now keeps its default, and a file that
|
|
356
|
+
/// still cannot be read says so in the log.
|
|
357
|
+
static func load(from data: Data) -> CraftConfig {
|
|
358
|
+
let defaults = (try? JSONEncoder().encode(CraftConfig()))
|
|
359
|
+
.flatMap { try? JSONSerialization.jsonObject(with: $0) as? [String: Any] } ?? [:]
|
|
360
|
+
guard let given = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else {
|
|
361
|
+
NSLog("[craft] craft.config.json is not a JSON object; running on defaults")
|
|
362
|
+
return CraftConfig()
|
|
363
|
+
}
|
|
364
|
+
let merged = defaults.merging(given.filter { !($0.value is NSNull) }) { _, bundled in bundled }
|
|
365
|
+
do {
|
|
366
|
+
return try JSONDecoder().decode(CraftConfig.self, from: JSONSerialization.data(withJSONObject: merged))
|
|
367
|
+
} catch {
|
|
368
|
+
NSLog("[craft] craft.config.json could not be read, running on defaults: %@", String(describing: error))
|
|
369
|
+
return CraftConfig()
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
351
374
|
// MARK: - Configuration
|
|
352
375
|
struct CraftConfig: Codable {
|
|
353
376
|
var appName: String = "Craft App"
|
|
@@ -514,6 +537,48 @@ final class BundledAssetSchemeHandler: NSObject, WKURLSchemeHandler {
|
|
|
514
537
|
}
|
|
515
538
|
}
|
|
516
539
|
|
|
540
|
+
#if DEBUG
|
|
541
|
+
/// Relays the page's console and uncaught errors to NSLog in debug builds.
|
|
542
|
+
final class PageConsoleRelay: NSObject, WKScriptMessageHandler {
|
|
543
|
+
static let script = """
|
|
544
|
+
(function() {
|
|
545
|
+
var handler = window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.craftLog;
|
|
546
|
+
if (!handler) return;
|
|
547
|
+
function text(value) {
|
|
548
|
+
try { return typeof value === 'string' ? value : (value && value.stack) || JSON.stringify(value); }
|
|
549
|
+
catch (e) { return String(value); }
|
|
550
|
+
}
|
|
551
|
+
function send(level, message) { try { handler.postMessage({ level: level, message: String(message).slice(0, 4000) }); } catch (e) {} }
|
|
552
|
+
['error', 'warn', 'log', 'info'].forEach(function(level) {
|
|
553
|
+
var original = console[level];
|
|
554
|
+
console[level] = function() {
|
|
555
|
+
send(level, Array.prototype.map.call(arguments, text).join(' '));
|
|
556
|
+
return original.apply(console, arguments);
|
|
557
|
+
};
|
|
558
|
+
});
|
|
559
|
+
// Capture phase: a script or stylesheet that fails to load fires its
|
|
560
|
+
// error on the element, which never bubbles to window.
|
|
561
|
+
window.addEventListener('error', function(event) {
|
|
562
|
+
var target = event.target;
|
|
563
|
+
if (target && target !== window && (target.src || target.href)) {
|
|
564
|
+
send('error', 'failed to load ' + (target.src || target.href));
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
send('error', 'uncaught ' + event.message + ' at ' + event.filename + ':' + event.lineno + ':' + event.colno);
|
|
568
|
+
}, true);
|
|
569
|
+
window.addEventListener('unhandledrejection', function(event) {
|
|
570
|
+
send('error', 'unhandled rejection ' + text(event.reason));
|
|
571
|
+
});
|
|
572
|
+
})();
|
|
573
|
+
"""
|
|
574
|
+
|
|
575
|
+
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
|
576
|
+
guard let body = message.body as? [String: Any] else { return }
|
|
577
|
+
NSLog("[craft page] %@: %@", body["level"] as? String ?? "log", body["message"] as? String ?? "")
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
#endif
|
|
581
|
+
|
|
517
582
|
struct CraftWebView: UIViewRepresentable {
|
|
518
583
|
let config: CraftConfig
|
|
519
584
|
|
|
@@ -528,6 +593,14 @@ struct CraftWebView: UIViewRepresentable {
|
|
|
528
593
|
let contentController = WKUserContentController()
|
|
529
594
|
contentController.add(context.coordinator, name: "craft")
|
|
530
595
|
webConfig.userContentController = contentController
|
|
596
|
+
#if DEBUG
|
|
597
|
+
// The page's errors and console, in the device log, so a debug build
|
|
598
|
+
// can be diagnosed from `log stream` or `simctl spawn … log show`
|
|
599
|
+
// without attaching Safari. A separate handler, outside the bridge's
|
|
600
|
+
// action dispatch. Never in a release build.
|
|
601
|
+
contentController.add(PageConsoleRelay(), name: "craftLog")
|
|
602
|
+
contentController.addUserScript(WKUserScript(source: PageConsoleRelay.script, injectionTime: .atDocumentStart, forMainFrameOnly: true))
|
|
603
|
+
#endif
|
|
531
604
|
|
|
532
605
|
let webView = WKWebView(frame: .zero, configuration: webConfig)
|
|
533
606
|
webView.navigationDelegate = context.coordinator
|
|
@@ -1630,12 +1703,32 @@ struct CraftWebView: UIViewRepresentable {
|
|
|
1630
1703
|
decisionHandler(.allow)
|
|
1631
1704
|
return
|
|
1632
1705
|
}
|
|
1706
|
+
// An iframe the app's page embeds loads in place. Only the main
|
|
1707
|
+
// frame's navigations leave for Safari: cancelling the frame's
|
|
1708
|
+
// too left every embed blank, a YouTube or Vimeo player among them.
|
|
1709
|
+
if let frame = navigationAction.targetFrame, !frame.isMainFrame, isEmbeddableFrameURL(url) {
|
|
1710
|
+
decisionHandler(.allow)
|
|
1711
|
+
return
|
|
1712
|
+
}
|
|
1633
1713
|
if navigationAction.targetFrame?.isMainFrame != false {
|
|
1634
1714
|
UIApplication.shared.open(url)
|
|
1635
1715
|
}
|
|
1636
1716
|
decisionHandler(.cancel)
|
|
1637
1717
|
}
|
|
1638
1718
|
|
|
1719
|
+
/// What an iframe inside the app's page may load: a video player, a
|
|
1720
|
+
/// payment form, a map. It stays inside its frame, and the bridge
|
|
1721
|
+
/// answers only `isTrustedOrigin`, so loading one reaches nothing
|
|
1722
|
+
/// native. Plain http is left to the trusted local origins.
|
|
1723
|
+
private func isEmbeddableFrameURL(_ url: URL) -> Bool {
|
|
1724
|
+
switch url.scheme?.lowercased() {
|
|
1725
|
+
case "https", "about", "data", "blob":
|
|
1726
|
+
return true
|
|
1727
|
+
default:
|
|
1728
|
+
return false
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1639
1732
|
func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
|
|
1640
1733
|
webView.reload()
|
|
1641
1734
|
}
|