# iOS Host App

This directory contains the native SwiftUI host for the Compose Multiplatform app.

Open `iosApp.xcodeproj` or use the shared `iosApp` scheme from Android Studio. The `BuildSharedFramework.sh` build phase calls:

```bash
./gradlew :composeApp:embedAndSignAppleFrameworkForXcode
```

The generated `YokanShared.framework` is imported by `YokanApp.swift`, which mounts the shared `MainViewController()`.

## TestFlight with Codemagic

The `ios-testflight` workflow in `../codemagic.yaml` archives the shared `iosApp`
scheme in Release, signs it for App Store distribution, uploads the IPA, and
distributes it to the **Yokan Testers** TestFlight group. It runs on pushes to
`main` and can also be started manually in Codemagic. It does not submit an App
Store release.

Configure these once before the first run. All setup can be done from Linux;
Codemagic supplies the macOS build machine and Xcode.

1. Use an active Apple Developer Program membership. In Apple Developer →
   Certificates, Identifiers & Profiles, register an explicit App ID with bundle
   ID `eu.friedcutlet.yokan`. In App Store Connect → My Apps → **+ → New App**,
   create **Yokan**, select iOS and that bundle ID, choose a primary language,
   and enter a unique SKU such as `yokan-ios`. Record its numeric **Apple ID**
   from **App Information**. Use the same Apple team throughout signing.
2. In Forgejo → your avatar → **Settings → Applications → Generate New Token**,
   create a dedicated token named `codemagic-yokan`. Grant **Repository → Read**
   (`read:repository`). If your Forgejo version offers **Specific repositories**,
   select `friedcutlet/yokan`, `friedcutlet/kmp-mpv`, and `friedcutlet/yokan-ext`;
   otherwise allow private repository access. The account creating the token
   must have read access to all three. Copy the generated token immediately;
   Forgejo displays it only once. No repository write or administrator scope is
   needed for these builds.
3. In Codemagic, select your team (enable billing if the team setup requires it)
   and choose **Add application → Other**. Enter
   `https://git.friedcutlet.com/friedcutlet/yokan.git`, select private repository
   authentication, and enter your Forgejo username with the token from step 2
   as the **Password**. Choose the non-Flutter/Other project type and use the
   root `codemagic.yaml`. For an existing app, update **App settings → Repository
   settings** with these HTTPS credentials. Keep the URL free of credentials.
4. In Codemagic's environment variables, create the group
   `private_git_credentials`. Add **`FORGEJO_USERNAME`** with your Forgejo login
   (for example `friedcutlet`), and **`FORGEJO_TOKEN`** with the token from step
   2, marked **Secret**. Remove the old `FORGEJO_SSH_KEY` variable if migrating
   from SSH. The connection in step 3 fetches the main repository before scripts
   run; these variables authenticate the private HTTPS submodules through
   `scripts/init-private-submodules.sh`. Both settings are required.
5. In App Store Connect → **Users and Access → Integrations → App Store Connect
   API → Team Keys**, generate a dedicated key with **App Manager** access.
   An Account Holder may need to enable API access first. Download its `.p8`
   file (available once), and note the issuer ID and key ID. In Codemagic
   **Team settings → Team integrations → Developer Portal → Manage keys**, add
   those values and the `.p8` file. Name the integration **`yokan-testflight`**
   exactly, matching the YAML.
6. In Codemagic **Team settings → codemagic.yaml settings → Code signing
   identities → iOS certificates**, select **Generate certificate**, choose
   **Apple Distribution**, select `yokan-testflight`, and give it a reference
   name such as `yokan_distribution`. Alternatively upload a `.p12` containing
   both an existing distribution certificate and its private key, plus its
   password if required. A `.cer` file alone is insufficient.
7. In Apple Developer → **Profiles → +**, create an **App Store Connect**
   distribution profile for `eu.friedcutlet.yokan`, selecting the certificate
   from step 6. Download it. In Codemagic's **iOS provisioning profiles** tab,
   upload the `.mobileprovision` file, or use **Fetch profiles** to import it.
   Give it a reference name such as `yokan_app_store`; verify its matching
   certificate has a green checkmark. The YAML selects these files by bundle ID
   and distribution type, so the reference names are your choice.
8. Create a variable group named **`ios_testflight`** containing
   **`APP_STORE_APPLE_ID`**, set to the numeric Apple ID from step 1. This is
   the app's identifier in App Store Connect, not your Apple account email or
   the bundle ID. Ensure both variable groups are available to this app.
9. In App Store Connect → Yokan → **TestFlight**, create an internal testing
   group named **`Yokan Testers`** exactly. Add eligible App Store Connect users
   (invite them through **Users and Access** first if necessary). Enable
   automatic distribution to the group when offered. Complete required beta
   information and export compliance questions. External testers require an
   external group and TestFlight App Review instead.
10. In Codemagic, choose **Start new build**, branch **`main`**, and workflow
    **iOS TestFlight** (`ios-testflight`). Check source fetching, signing,
    archiving, and publishing logs. After Apple's processing completes, resolve
    any **Missing Compliance** prompt and confirm the build is available to
    **Yokan Testers**. Install Apple's **TestFlight** app on the iPhone and
    accept the tester invitation.

For automatic push builds, open the app's **Webhooks** tab in Codemagic and copy
its payload URL, `https://api.codemagic.io/hooks/<appId>`. In Forgejo → Yokan →
**Settings → Webhooks → Add webhook → Forgejo** (or Gitea on older instances),
set that URL, method **POST**, content type **application/json**, **Push Events**,
branch filter **main**, and **Active**. Match any webhook secret configured in
Codemagic. Test delivery and inspect Codemagic's received webhooks and build
list. Forgejo supplies GitHub-compatible event headers, but Codemagic does not
document Forgejo as a supported provider; confirm an actual `main` push starts
the workflow before relying on it. Manual builds work independently of webhooks.
The existing sideload and macOS workflows also match `main` pushes; remove their
`triggering` sections if you only want automatic TestFlight builds.

The workflow reads the
marketing version from `../version.xcconfig` (or `YOKAN_VERSION`) and chooses a
build number greater than all existing TestFlight builds, with the repository's
derived build number as its minimum. Authentication or build-number lookup
failures stop the build. Run one TestFlight build at a time so parallel runs do
not choose the same next number. Both Gradle and Xcode receive the same version
and build-number overrides.

TestFlight builds disable `YOKAN_SIDELOAD_RUNTIME` and omit the embedded JVM and
extension-server JAR. On iOS, profiles without a saved extension-server URL use
`https://bridge.mangayomi.30062022.xyz`; an explicitly saved URL takes precedence.
Android, desktop, and web retain their existing defaults.

Both Codemagic iOS workflows use Java 21 and preserve `~/.konan` alongside the
Gradle caches. Kotlin/Native release binary caching is enabled in
`../gradle.properties` to compile dependencies separately and reduce the heap
needed by the final framework link on the 8 GB M2 runner. The first build must
populate these caches. This mode trades some whole-program optimization for
lower build memory use; see [Kotlin's release caching guidance](https://kotlinlang.org/docs/native-improving-compilation-time.html#enable-caches-for-release-binaries).
The two cache properties must be kept together when updating Kotlin, including
the documented internal Gradle plugin property. The Xcode Gradle phase includes
stack traces so a failure's compiler stage is visible in the build log.

See [adding custom repositories](https://docs.codemagic.io/getting-started/adding-apps/),
[Forgejo access tokens](https://forgejo.org/docs/latest/user/authentication/token-scope/),
[iOS signing](https://docs.codemagic.io/yaml-code-signing/signing-ios/),
[App Store Connect publishing](https://docs.codemagic.io/yaml-publishing/app-store-connect/),
[Codemagic webhooks](https://docs.codemagic.io/configuration/webhooks/), and
[Forgejo webhooks](https://forgejo.org/docs/latest/user/repository/webhooks/).

## Sideloaded extension server

Sideloaded device builds embed Mangatan's pinned OpenJDK Zero framework and
iOS-specific M-Extension-Server JAR. The framework is loaded only when the
server starts, runs without a JIT entitlement, and pauses its loopback listener
while the app is backgrounded. Simulator and App Store builds omit the runtime.

Device builds include the Java runtime and M-Extension-Server only when the
`YOKAN_SIDELOAD_RUNTIME=YES` build setting is present. This mode is for
sideloading and is intentionally excluded from ordinary distribution builds:

```bash
xcodebuild -project iosApp.xcodeproj -scheme iosApp \
  -destination 'generic/platform=iOS' \
  YOKAN_SIDELOAD_RUNTIME=YES YOKAN_SIDELOAD_RUNTIME_ENABLED=1 build
```
