跪拜 Guibai
← All articles
iOS · JavaScript

Run JavaScript on iPhone: Scriptable Plus a 3-Line Live-Reload Script

By Vic_wkx ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

JavaScript developers get a free execution target on iOS without touching Xcode. The three-line loader removes the copy-and-paste loop that kills phone scripting, and the timestamped-URL plus async-eval pattern transfers to any device that can fetch and run a string from a local server.

Summary

Scriptable is an iOS app that runs JavaScript on the phone, and scripts can be added to the home screen as widgets. A minimal Alert script with title, message, and an OK button shows how little setup a first script needs; the result behaves like a PWA, web-built but installed from the home screen. Long-press the desktop, add the widget, then long-press it to pick which script runs.

The edit loop is the pain point: code written in VS Code gets pasted through a chat app to reach the phone, and editing on the device is unfriendly. LAN hot reload is the fix. A Python one-liner, `python -m http.server 8080`, serves the code folder from the computer, and a permanent three-line loader on the phone builds a Request to `http://{LAN-IP}:8080/my-script.js?t=${Date.now()}`, loads it as a string, and evaluates it.

The timestamped URL makes each request unique, defeating the iOS HTTP cache so the phone always gets the newest file. Fetched code is then wrapped in an immediately-invoked async arrow function inside eval(), because native eval runs synchronously and can't hold top-level await, while Scriptable expects an async context. A verification ladder runs from terminal output to a desktop browser to a phone-browser fetch of `http://{computer-ip}:8080`, with a `GET / HTTP/1.1 200` in the server log as final proof.

Takeaways
— Scriptable runs JavaScript on iOS and exposes scripts as home-screen widgets, so a web-built app installs like a native PWA.
— A first script is an Alert with a title, message, and an action, shown with await alert.present().
— The edit pain is real: code written in VS Code travels to the phone through a chat app, and editing JS on the phone itself is awkward.
— The hot-reload setup is `python -m http.server 8080` on the computer plus a three-line loader script kept permanently on the phone.
— The loader's URL ends in `?t=${Date.now()}`, which defeats iOS's HTTP cache by making every request unique.
— eval() alone cannot handle top-level await, so the fetched code is executed inside an immediately-invoked async arrow function to match Scriptable's environment.
— Server verification runs from terminal output, to `localhost:8080` in a desktop browser, to a phone-browser fetch of `http://{computer-ip}:8080` with a `200` in the server log.
Conclusions

The opening premise is the notable part: handing tasks to AI felt like "making wishes" rather than programming, so the response was to build something by hand. Technical content here is deliberately basic; the act of building is the point.

Worth stealing from the loader: timestamped URLs plus an async IIFE inside eval() solve both iOS caching and the top-level-await limitation in four lines.

Proving the network path matters more than starting the server: terminal output and a desktop check only confirm Python is running, while the phone-browser test with a server-log 200 actually proves the device can reach the machine.

Concepts & terms
Scriptable
An iOS app that runs JavaScript on the phone, including as home-screen widgets. It provides its own runtime with async APIs like Alert.present().
PWA (Progressive Web App)
An app built with web technologies (HTML/CSS/JS) that can be installed to a phone's home screen like a native app, without going through an app store.
Cache-busting query parameter
Appending ?t=${Date.now()} to a URL makes each request unique, forcing the client to skip its HTTP cache and fetch the current version of the file.
Async IIFE in eval()
An immediately-invoked async arrow function, (async () => {...})(), wraps fetched code so top-level await works inside eval(), which natively runs synchronously.
From the discussion
Featured comments
组合搭配函数

This trick is really handy; the hot-reload part is the key. Change on the computer, and the phone runs it right away — this is what learning iOS should look like.

Vic_wkx

Yes, and another trick: while the phone is running, when printing logs for debugging, transmit the logs over the LAN to a file on the computer, so the phone runs while the computer shows the logs — even smoother for development. I'll add this content later.

Vic_wkx

Updated the way to view phone runtime logs on the computer: https://juejin.cn/spost/7712284408648695843

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗