Skip to content

Your home as a live 3D model in the Mac menu bar and on iPhone — lights and ACs via Smart Life, Home Assistant, Philips Hue and Apple Home.

iPhone: make the web view when it first goes on screen; build 9

0xProbabilityd4982627c0cf
diff --git a/ios/Config/Base.xcconfig b/ios/Config/Base.xcconfig
index 4d40e0b..cc4c108 100644
--- a/ios/Config/Base.xcconfig
+++ b/ios/Config/Base.xcconfig
@@ -1,6 +1,6 @@
// Version of the iPhone app. CURRENT_PROJECT_VERSION is the build number TestFlight sees.
MARKETING_VERSION = 1.0.0
-CURRENT_PROJECT_VERSION = 7
+CURRENT_PROJECT_VERSION = 9
CODE_SIGN_STYLE = Automatic
diff --git a/ios/Sources/HomeWeb.swift b/ios/Sources/HomeWeb.swift
index 04b6430..06f3545 100644
--- a/ios/Sources/HomeWeb.swift
+++ b/ios/Sources/HomeWeb.swift
@@ -8,10 +8,11 @@ import HouseCore
/// editor ("editor", the Mac's editor window laid out for a phone).
@MainActor
final class HomeWeb: NSObject, WKScriptMessageHandler, WKNavigationDelegate {
- let webView: WKWebView
let mode: String
var onMessage: (([String: Any]) -> Void)?
- private var boot: () -> [String: Any] = { [:] }
+ private let configuration: WKWebViewConfiguration
+ private var view: WKWebView?
+ private var boot: (() -> [String: Any])?
/// Set by the page's "ready" message; a load that never gets there is retried.
private var ready = false
private var retries = 0
@@ -24,38 +25,48 @@ final class HomeWeb: NSObject, WKScriptMessageHandler, WKNavigationDelegate {
init(persistent: Bool, mode: String = "panel") {
self.mode = mode
- let config = WKWebViewConfiguration()
- config.setURLSchemeHandler(AssetSchemeHandler(root: Self.assetRoot), forURLScheme: AssetSchemeHandler.scheme)
- config.allowsInlineMediaPlayback = true
+ configuration = WKWebViewConfiguration()
+ configuration.setURLSchemeHandler(AssetSchemeHandler(root: Self.assetRoot), forURLScheme: AssetSchemeHandler.scheme)
+ configuration.allowsInlineMediaPlayback = true
// A run against a throw-away data folder must not touch the saved panel state either.
- if !persistent { config.websiteDataStore = .nonPersistent() }
- webView = WKWebView(frame: .zero, configuration: config)
+ if !persistent { configuration.websiteDataStore = .nonPersistent() }
super.init()
- config.userContentController.add(self, name: "house")
+ configuration.userContentController.add(self, name: "house")
+ }
+
+ /// The web view, made the first time SwiftUI puts it on screen. One made earlier, before the
+ /// app has its window (the model is created at launch), aborts the app on the first touch on
+ /// iOS 17: UIKit's gesture graph misses a node for WebKit's gestures
+ /// ("Invalid parameter not satisfying: targetNode").
+ var webView: WKWebView {
+ if let view { return view }
+ let webView = WKWebView(frame: .zero, configuration: configuration)
webView.navigationDelegate = self
webView.isOpaque = false
webView.backgroundColor = UIColor(Theme.background)
webView.scrollView.backgroundColor = UIColor(Theme.background)
webView.scrollView.isScrollEnabled = false
- // The page itself keeps text fields from zooming it in (`maximum-scale=1` in index.html).
- // Never disable the scroll view's pinch recognizer for that: WebKit's own gestures depend
- // on it, and on iOS 17 a disabled one leaves a hole in UIKit's gesture graph that aborts
- // the app on the first touch.
+ // The page keeps text fields from zooming it in (`maximum-scale=1` in index.html).
webView.scrollView.bounces = false
webView.scrollView.contentInsetAdjustmentBehavior = .never
webView.allowsLinkPreview = false
#if DEBUG
webView.isInspectable = true
#endif
+ view = webView
+ if boot != nil { reload() }
+ return webView
}
+ /// Loads the page now if the web view is on screen, else as soon as it is.
func load(boot: @escaping () -> [String: Any]) {
self.boot = boot
- reload()
+ if view != nil { reload() }
}
func reload() {
- let controller = webView.configuration.userContentController
+ guard let view, let boot else { return }
+ let controller = configuration.userContentController
controller.removeAllUserScripts()
var payload = boot()
payload["mode"] = mode
@@ -63,7 +74,7 @@ final class HomeWeb: NSObject, WKScriptMessageHandler, WKNavigationDelegate {
controller.addUserScript(WKUserScript(source: "window.__HOUSE_BOOT__ = \(WebJSON.literal(payload));",
injectionTime: .atDocumentStart, forMainFrameOnly: true))
controller.addUserScript(WKUserScript(source: Self.errorReporter, injectionTime: .atDocumentStart, forMainFrameOnly: true))
- webView.load(URLRequest(url: URL(string: "\(AssetSchemeHandler.indexURL)?mode=\(mode)")!))
+ view.load(URLRequest(url: URL(string: "\(AssetSchemeHandler.indexURL)?mode=\(mode)")!))
// Right after a cold boot WebKit's processes can be slow enough to drop the first
// load; a page that has not reported in is loaded again, a couple of times at most.
@@ -90,13 +101,13 @@ final class HomeWeb: NSObject, WKScriptMessageHandler, WKNavigationDelegate {
/// Stops the page from talking back (the message handler holds on to this object).
func detach() {
watchdog?.cancel()
- webView.configuration.userContentController.removeScriptMessageHandler(forName: "house")
- webView.stopLoading()
+ configuration.userContentController.removeScriptMessageHandler(forName: "house")
+ view?.stopLoading()
}
/// Calls `window.house.<function>(…args)` in the page.
func call(_ function: String, _ args: Any...) {
- webView.evaluateJavaScript(WebJSON.call(function, args), completionHandler: nil)
+ view?.evaluateJavaScript(WebJSON.call(function, args), completionHandler: nil)
}
func reply(to message: [String: Any], _ result: Any) {