Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion apps/native/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ finishes at the finger's speed, the runtime drawing where it goes while the page
press on the tab bar never waits on a picture: the bar and the page stay live while the tab draws.

**Live data** (`HAZEL_DATA=live bun run ios`, with `VITE_APP_URL`/`VITE_BACKEND_URL` for a dev
stack on this Mac) signs in as the Mac app does: the sign-in link opens in the system's sign-in
stack on this Mac, and `HAZEL_SESSION_TOKEN` from `dev:sign-in` for a PR preview, which has no
OAuth) signs in as the Mac app does: the sign-in link opens in the system's sign-in
sheet (`ASWebAuthenticationSession`, `host/ios/src/sign_in.rs`), whose callback goes to the
runtime as an opened link, and the session lives in the keychain (`hazel_native::sign_in`). The
simulator's keychain serves only an app with an identifier and access group, which the
Expand All @@ -164,6 +165,28 @@ over its box. A finger on an inline page also reaches Rune's view, so a drag ove
list and a long press opens the message's menu, while a tap presses what is under it in the page; a
fullscreen page scrolls itself.

**Push notifications** (`host/ios/src/push.rs`, `src/push.ts`). The app asks for notifications
through the web app's own flow (`host/src/notify.rs`, as on the Mac; Settings > Notifications'
test asks offline, live asks at start), for alerts, sounds and the badge. Once allowed, and again
on every launch, it registers with APNs; the token goes to every runtime, which registers it for
the signed-in user (`pushDevice.register`, with the build's APNs environment and the bundle ID).
Signing in loads a fresh runtime, which registers it then; signing out forgets it first
(`web-foldkit/src/platform/push-device.ts`). Offline, nothing is registered. Winit runs
`UIApplicationMain` without an application delegate, so the app sets its own for APNs's two
answers. A push shows while the app is away and nothing while it is in front (the app shows its
own); a tap opens the app at its `hazel.path`, a cold start's first page loads there. Becoming
active clears the badge. The simulator's executable embeds `aps-environment` `development`
(`host/ios/build.rs`): an Apple-silicon simulator gets a real sandbox token. A release is signed
with `production`, which its provisioning profile must grant (the App ID's Push Notifications
capability; `scripts/ios.ts` stops otherwise). The log has each step (`[push] ...`: the
permission, the token's first 8 characters, the registration, a tap's path). To try it:

```sh
xcrun simctl push <udid> sh.hazel.ios payload.json
# {"aps":{"alert":{"title":"Maki · #engineering","body":"Can you look at the deploy?"},"badge":3},
# "hazel":{"path":"/hazel/chat/00000005-0000-4000-8000-000000000002"}}
```

Not yet on iOS: updates.

### TestFlight
Expand Down
2 changes: 2 additions & 0 deletions apps/native/host/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions apps/native/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ objc2-web-kit = "0.3"
objc2-quartz-core = "0.3"
objc2-core-graphics = "0.3"
objc2-core-foundation = "0.3"

[target.'cfg(target_os = "ios")'.dependencies]
objc2-ui-kit = "0.3"

[target.'cfg(target_os = "macos")'.dependencies]
objc2-app-kit = "0.3"
# System notifications (`src/notify.rs`): the web app's on macOS, the permission for pushes on iOS.
objc2-user-notifications = { version = "0.3", default-features = false, features = [
"std",
"UNUserNotificationCenter",
Expand All @@ -62,6 +57,12 @@ objc2-user-notifications = { version = "0.3", default-features = false, features
"block2",
] }

[target.'cfg(target_os = "ios")'.dependencies]
objc2-ui-kit = "0.3"

[target.'cfg(target_os = "macos")'.dependencies]
objc2-app-kit = "0.3"

[profile.release]
debug = false

2 changes: 2 additions & 0 deletions apps/native/host/ios/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ resvg = "0.45"
objc2-quartz-core = "0.3"
objc2-ui-kit = "0.3"
objc2-web-kit = "0.3"
# Pushes: the notification center's delegate and the badge (`src/push.rs`).
objc2-user-notifications = "0.3"
5 changes: 4 additions & 1 deletion apps/native/host/ios/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! The simulator takes an app's entitlements from its executable (`__TEXT,__entitlements`), as
//! Xcode's simulator builds embed them: the keychain (the sign-in vault) serves only an app with
//! an identifier and an access group. An ad-hoc signature carries none.
//! an identifier and an access group. An ad-hoc signature carries none. `aps-environment` lets it
//! register for pushes: an Apple-silicon simulator gets a sandbox (`development`) token.
use std::path::PathBuf;

/// The team prefix of an ad-hoc signed simulator build: any ten characters do.
Expand All @@ -24,6 +25,8 @@ fn main() {
<dict>
<key>application-identifier</key>
<string>{id}</string>
<key>aps-environment</key>
<string>development</string>
<key>keychain-access-groups</key>
<array>
<string>{id}</string>
Expand Down
67 changes: 61 additions & 6 deletions apps/native/host/ios/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,39 @@ thread_local! {
/// Where the last full page load went; a restart goes back there.
static LAST_PATH: RefCell<Option<String>> = const { RefCell::new(None) };
static RESTARTS: RefCell<Restarts> = RefCell::new(Restarts::default());
/// This device's push token (`push.rs`): every runtime hears it once its page is ready.
static PUSH_TOKEN: RefCell<Option<serde_json::Value>> = const { RefCell::new(None) };
/// The path a notification's tap opens, until a runtime says it followed it (`openedPath`):
/// a page that ends before then (WebKit ended its process in the background) gets it again.
static OPEN_PATH: RefCell<Option<String>> = const { RefCell::new(None) };
}

/// Whether the page loaded last can take lines and replies.
fn page_ready() -> bool {
EARLY.with_borrow(Option::is_none)
}

/// This device's push token, for the runtime to register (`src/push.ts`): now, and again for
/// every runtime a page load starts (signing in loads one). APNs answers every activation; the
/// same token still goes to the runtime, which retries a registration that failed and calls the
/// API only for a token it has not registered. Whether the token is new.
pub fn set_push_token(message: serde_json::Value) -> bool {
if page_ready() {
reply(&message);
}
let new = PUSH_TOKEN.with_borrow(|held| held.as_ref() != Some(&message));
PUSH_TOKEN.set(Some(message));
new
}

/// Opens the app at `path` (a notification's tap): the runtime follows it as a link once it is
/// up, and the path is held until it says so; a cold start from the tap loads its first page
/// there.
pub fn open_path(path: String) {
if page_ready() {
reply(&serde_json::json!({ "openPath": path }));
}
OPEN_PATH.set(Some(path));
}

/// Where the runtime's trees go: the running app.
Expand Down Expand Up @@ -214,11 +247,13 @@ fn receive(line: &str) {
Routed::Shell(ShellRequest::Update { .. } | ShellRequest::Relaunch { .. }) => {
log("[host] updates are not on iOS");
}
// Not offered on iOS (no `HAZEL_NOTIFICATIONS`); a request still gets its answer.
Routed::Shell(ShellRequest::NotifyPermission { id, .. }) => {
outbox::write_line(shell::permission_reply(id, false));
// The web app's notification flow: its permission is the one pushes need.
Routed::Shell(ShellRequest::NotifyPermission { id, request }) => {
hazel_native::notify::permission(id, request, crate::push::after_permission);
}
Routed::Shell(ShellRequest::Notify { title, body, group }) => {
hazel_native::notify::show(&title, &body, group.as_deref());
}
Routed::Shell(ShellRequest::Notify { .. }) => {}
}
}

Expand All @@ -238,7 +273,20 @@ fn request(mtm: MainThreadMarker, body: &NSDictionary<NSString, AnyObject>) {
for text in EARLY.take().unwrap_or_default() {
to_worker(&text);
}
if let Some(token) = PUSH_TOKEN.with_borrow(Clone::clone) {
reply(&token);
}
if let Some(path) = OPEN_PATH.with_borrow(Clone::clone) {
reply(&serde_json::json!({ "openPath": path }));
}
}
} else if let Some(path) = field("openedPath") {
// The runtime followed the tap's path: a page that ends from now on need not.
OPEN_PATH.with_borrow_mut(|held| {
if held.as_deref() == Some(path.as_str()) {
*held = None;
}
});
} else if let Some(line) = field("log") {
log(&line);
} else if let (Some(url), Some(scheme)) = (field("signIn"), field("callbackScheme")) {
Expand Down Expand Up @@ -311,7 +359,11 @@ define_class!(
return log("[host] the page's process keeps ending; not starting it again");
}
log("[host] the page's process ended; starting the runtime again");
let path = LAST_PATH.with_borrow(Clone::clone);
// A tap the old runtime never followed (the process ended first) leads instead.
let path = OPEN_PATH.take().or_else(|| LAST_PATH.with_borrow(Clone::clone));
if path.is_some() {
LAST_PATH.set(path.clone());
}
load_page(self.mtm(), path.as_deref());
}
}
Expand Down Expand Up @@ -378,7 +430,10 @@ fn start() -> bool {
}
NAVIGATION.set(Some(navigation));
WEB.set(Some(web));
load_page(mtm, None);
// A tap on a notification that launched the app: the first page opens where it leads.
let path = OPEN_PATH.take();
load_page(mtm, path.as_deref());
LAST_PATH.set(path);
if let Some(handle) = HANDLE.get().map(|h| h.lock().unwrap().clone()) {
crate::keyboard::observe(mtm, handle);
}
Expand Down
6 changes: 6 additions & 0 deletions apps/native/host/ios/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ mod menu_sheet;
#[cfg(any(test, target_os = "ios"))]
mod page;
#[cfg(target_os = "ios")]
mod push;
#[cfg(target_os = "ios")]
mod sign_in;
#[cfg(target_os = "ios")]
mod system;
Expand Down Expand Up @@ -42,6 +44,10 @@ fn main() {
// The UIKit views the app's `data-native` elements get, and HTML renders' web views, go
// where their boxes were drawn, after every frame.
islands::install();
// Before the app runs, as Apple asks: a tap on a notification that launches it is heard.
if let Some(mtm) = objc2::MainThreadMarker::new() {
push::install(mtm);
}
let app = App::new(spec)
.font_smoothing(TEXT_SMOOTHING)
.on_frame_drawn(|runtime| {
Expand Down
Loading
Loading