iPhone
The iPhone app exists so EmberStorm can be on the App Store rather than only a home-screen web app (for now it is a public beta through TestFlight, not yet on the store). It is a native Swift shell around the server's own page, with songs and audiobooks played by the app's own native player - and a deliberate decision not to rewrite the app in Swift.
Three shapes, and the one chosen
Three ways of building it were weighed:
- A thin wrapper - a native shell that loads the server's web app.
- A wrapper whose audio is native - the same shell, with music and audiobooks played by iOS itself rather than by the page.
- A fully native app, rebuilt in Swift.
The second was chosen, built in two stages.
app.js is the whole product across five kinds of media. A Swift rewrite
would mean building every feature twice from then on - and Android would still be on the web. What
only native code can give on a phone is CarPlay, audio that iOS will not stop, and the lock screen:
all of it audio. So only the audio goes native. Nothing commits the shell to staying thin; it can
grow screen by screen later if that ever looks worth it.The reasoning that ruled out a WebView app on Android does not carry over. Android's WebView has
no Media Session API and is stopped in the background; an iPhone's WKWebView is Safari's
engine, and with background audio enabled (UIBackgroundModes) and the
.playback audio session, the page's <audio> keeps playing when the
phone locks. That turned out not to be enough (see stage two).
Stage one: the shell
Plain Swift in ios/SoundStorm, with no Capacitor. Capacitor is built to bundle a web
app inside the app, and this web app lives on each person's own server; stage two is Swift anyway.
What the shell does:
- Asks for the server's address on first launch, since every install is somebody's
own, and checks that
/healthzanswers like EmberStorm before keeping it. With no scheme typed it assumes https. - Shows the page full screen, laid out under the status bar with the same
env(safe-area-inset-*)values the installed web app uses. - Shows
alert,confirmandprompt- aWKWebViewshows none of them unless its app does, and EmberStorm asks before removing downloads. - Sends links off the server to Safari, but only for a link that was actually tapped in the main frame; other schemes and new windows are not opened on a page's say-so.
- Sets
window.soundstormAppbefore the page runs, which is howapp.jsknows to show Change server beside Sign out. - Allows plain http only on the local network
(
NSAllowsLocalNetworking).
Messages from the page are accepted only from the server's own origin, port included - anyone can
get a *.net.emberstorm.app name, so a matching host alone is not enough.
Moving to the secure name
The first TestFlight build had a bug the Android app had had too. Opened at the away-from-home
name while at home, the page moves itself to the install's home name
(moveToSecureName - the server offers it to any page not already on it), and the app
took that move for a link out of EmberStorm and sent it to Safari.
The app now follows a move to https on <id>.home.emberstorm.app, on the same
port, where the id is the current name's own. It follows the move for this launch and does not
save it. A phone leaves the house; with the home name saved, the app would stop working the
moment it did. Not saving also closes a hole a security review found: from a plain-http address,
anybody on the same Wi-Fi could otherwise have pinned the app to a server of their own. A move to
a different name of the same install - its owner's chosen name, such as
yourname.home.emberstorm.app - is followed only when both addresses' /healthz
answer with the same install id - and, from a plain-http address, only when both names resolve to the
same machine, since an id is only what a server says of itself.
The status bar in Now Playing
The status bar hides while Now Playing is open, and only then - the owner's choice. Android hides
both bars everywhere, but an iPhone has no swipe that brings a hidden status bar back for a glance at
the time, so everywhere else it stays. The app watches the page's body for the
np-open class from its document-start script rather than being told by
app.js, so the feature needed no change to the web app and no server deploy.
Downloads kept by the app, and opening offline
A WKWebView runs a service worker only for App-Bound Domains, and listing the two
zones there would have shut the app out of every other address (a new box's plain first page, a typed
IP, a Tailscale name). And iOS may clear a web view's storage - where a browser keeps downloads - when
the phone is short of space or a site goes unused. So the app keeps downloads itself
(KeptFiles.swift), in its own folder (Application Support/Kept/<host_port>/),
which iOS never clears and which is left out of iCloud backups:
- The page hands every download to the app (
soundstormApp.keep); the app fetches each file in a background session with the session's cookies, never following a redirect off the server, so a download finishes with the app put away. - The page reads a kept file back at
emberstorm-kept://<host:port>/<path>, which the app answers itself with the file's type and byte ranges. - A kept song or audiobook is played by the app's own player from its file, with the lock screen.
- When the server cannot be reached, the app opens the page's own kept copy on its downloads, with an offline bar and Change server, from whichever of the install's addresses holds them. It looks for the server by itself (every 15 seconds for two minutes, then every minute, and at once on coming to the front or the network returning) and loads it again as soon as it answers. A certificate refused or a crashed page reaches the same fallback.
Stage two: native audio
Reported from a real iPhone: paused on the lock screen, the music could not be started again
there. The page played it in the web view, and iOS suspends an app playing nothing a few seconds
after a pause - the web view with it - so the lock screen's play had nothing awake to answer. So the
app does what the Android app does: NativeAudio.swift, an AVQueuePlayer
behind a stand-in on the page's <audio id="audio-player">, speaking the same
"audio" messages as Android's, so the page needed no change.
- Play, pause and the seek bar on the lock screen are the player's own
(
MPRemoteCommandCenter), answered with the page asleep; next, previous and a book's jumps go to the page's media session handlers. - The songs ahead are handed over, so the player moves into them by itself.
- A sleep timer is kept by the player too (
sleepAt), since a phone sleeps a page whose screen is off. - An audiobook's place is saved by the player itself (
soundstormApp.place) every ten seconds while playing and on pause, end and stop, so an hour listened with the screen off is not an hour back on the next device. - Crossfade is off in the app.
- A playback log (
PlayerLog.swift) can be sent from the developer's own install to diagnose dropouts.
Checked in the simulator. A real lock screen, a call or an alarm have not all been tried on a device.
Photo backup
The iPhone app backs up the camera roll to the person's own photo folder
(ios/SoundStorm/PhotoBackup.swift). It answers the page's
window.soundstormApp.backup(...) messages with the same status fields the Android app
reports, so the page has no iPhone code: the switch and its options (Wi-Fi only, videos, only while
charging) are the same Settings card.
Whose iPhone it is. A phone can back up to several servers, one account on each. On
each server the first person to sign in is asked once, "Is this your iPhone?" - yes only
lets them turn backup on later in Settings - and from then nobody else on that server sees a switch on
that phone; the person whose phone it is can let it go (This isn't my phone). Who is
signed in is asked of the server (/api/session), never taken from the page, and each
server's backup sends only while its own person is signed in there. The public demo account is never
asked.
- What it reads: the photo library, with full or limited access, asked for when backup is turned on. Newest first.
- Nothing twice: the server is the one record. The phone asks
POST /api/photos/backup/checkabout photos by name, month and size (the size Photos records, 0 for one held only in iCloud), so a reinstall or a new phone sends nothing the server has. To keep each run light it remembers which photos the server confirmed, and the server's marker of the person's photo folder (/api/photos/backup/marker): while the marker holds, only new or edited photos are checked, with a full check once a day or whenever the marker moves. - How it sends: each original is written to a file
(
PHAssetResourceManager, iCloud allowed; a Live Photo as its still and its moving part) and handed ahead to a backgroundURLSession- up to 200 files or 1 GB waiting - which uploads them toPUT /api/photos/backupwith the phone locked, at a pace iOS decides. It sends the web view's own session cookie - WKWebView keeps its cookies apart from URLSession's - to the server only, never through a redirect. - When: while the app is open, 30 seconds after a photo is added, and as a
BGProcessingTaskiOS starts when it chooses (needing power if "only while charging"). Wi-Fi only means a connection that is not expensive or constrained. - Where to: only the server the backup was turned on for and its away-from-home twin, over https; never a plain-http address, and a page's message cannot change it.
Checked against a local server in the simulator: the six sample photos arrived in the right year and month, byte for byte. The background task and an upload finishing with the app closed want a real iPhone over a night.
Add media in the app asks the app for files (Photos or Files,
soundstormApp.pickFiles), since a web view keeps its own picker's files to itself; the
page plans and asks as ever, and the app then sends them in the same kind of background session,
each to the server it was meant for. A Share extension puts EmberStorm in the system's
Share sheet: what is shared is copied into a shared app-group folder, and the app hands it to the page
for the same review the next time it opens (an extension may not open its app, so the sheet says to
open EmberStorm).
A TV's sign-in QR code opens the app itself (Universal Links on the install's names, and
soundstorm://link), but only for a server the app already knows, and the app can scan a
TV's code with its own scanner; a code that came by a link is typed, never accepted unseen. While the
phone controls a TV, its volume buttons turn the TV's volume.
Several saved servers
A phone may know several servers - their own, and a relative's elsewhere. The connect screen is
Your servers: the latest used first, the one in use ticked, a tap to switch, a hold
for Rename and Remove, and a way to add another. The list (ServerAddress.all, in
ios/Shared and used by the Apple TV app too) keeps each address and a name; a first name
is the address without .home.emberstorm.app. Sign-ins stay with each server, since
cookies belong to an address, so switching signs nobody out.
How it is tested
An XCUITest in ios/SoundStormUITests drives the app through connecting and signing
in, and skips itself when no server answers locally. simctl cannot type or tap, and
scripting the Simulator through System Events waits on a macOS permission prompt nobody sees, so the
UI test is how the app gets driven. Two small traps from building it: a centred stack view measures a
multi-line label as one line until preferredMaxLayoutWidth is set, and in the UI test the
keyboard's accessory bar sits over the web form's submit button, so the test presses Return instead -
which is also what a person does.
The iPhone and Apple TV apps share one bundle id, so Apple's Universal Purchase will make them a single App Store listing (see TVs).