---
title: "Answer a native call in Swift or Kotlin"
description: "Sound, MIDI and a transport are built in (guide, apps/metronome): write no module."
modificationDate: "2026-11-27"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Answer a native call in Swift or Kotlin

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/native-module: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

Put one `ExactModule` per app in `modules/apple/*.swift` (`let exactModule` names it; combine calls and views in one class).
On Android, put `class exact.app.Module : ExactModule()` in `modules/android/Module.kt`.

Declare device [grants](https://exact.expo.dev/recipes/permissions.md) in the activating source. On Apple, `device.photos-add purpose.savePhoto` derives `NSPhotoLibraryAddUsageDescription`. Fetch sources cannot use native.

```prompt Add a native module in Swift
Give the Exact app in this folder a native module in Swift for a platform API that has no view. Use https://github.com/expo/exact/blob/main/docs/recipes/native-module.md as the source for each step. The Exact checkout has the same page at docs/recipes/native-module.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me.
2. If I did not say which platform API the module calls, ask me before you write code. If it is sound, MIDI or a transport, stop and tell me: they are built in (`bun exact.mjs docs sound midi and media`), and the app needs no module. If it needs a view (a map, the camera), use the native-views recipe instead.
3. Write one `ExactModule` subclass in `modules/apple/<name>.swift`, and name it with `let exactModule`. Use `call` for an answer within 100 ms, and `later` for anything slower; it replies once with `send` or `fail`. Throw `ExactNativeRefusal` to refuse. Use the session's clock (`context.now()`, `context.after(ms)`), never a timer of your own. app.json needs nothing for the module.
4. In app.ts, ask the module from each source, as the recipe shows. When `native` is not `available` (the build, or a platform with no module), return a default value. The web has no `call`: use `later` there, answered only by `modules/web/index.js`.
5. In app.test.contract, add a test for what the app shows with the default value.
6. Run `bun exact.mjs ios` to build the Swift, then `bun exact.mjs verify ios` until it passes. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 7) and report it; do not keep trying.
7. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
8. Tell me what the module does, what the tests and verify reported, and how to see it by hand: `bun exact.mjs ios --run`. `print` output shows in `bun exact.mjs agent ios logs`; `NSLog` does not.
```

```swift
import Foundation

final class Thermal: ExactModule {
    override func call(_ request: [String: Any]) -> [String: Any] {
        if context.agent { return ["state": "nominal"] }
        let state = ProcessInfo.processInfo.thermalState
        return ["state": state == .nominal ? "nominal" : "elevated"]
    }
    override func later(_ request: [String: Any], reply: ExactReply) { reply.send(call(request)) }
}
let exactModule: ExactModule.Type = Thermal.self
```

```kotlin
package exact.app

import android.os.PowerManager
import dev.exact.canvas.ExactModule
import org.json.JSONObject

class Module : ExactModule() {
    override fun call(request: JSONObject): JSONObject {
        if (context.agent) return JSONObject().put("state", "nominal")
        val power = context.application.getSystemService(PowerManager::class.java)
        val state = when (power?.currentThermalStatus) {
            null -> "unknown"
            PowerManager.THERMAL_STATUS_NONE -> "nominal"
            else -> "elevated"
        }
        return JSONObject().put("state", state)
    }
}
```

`context.after(1000.0) { … }` follows the session clock; `context.changed("thermal")` refreshes `native.watch("thermal")`.
Apple: `context.file("app:/data/…")` (storage), `context.asset("foo.mp3")` (OTA `assets/`, any thread after connect), `context.wrote("app:/data")` (watchers/widgets).

```contract
shape Thermal
  state: string

component App
  resource thermal = thermalState() as shape Thermal else empty()
  action check
    refresh thermal
  view
    column
      if pending(thermal)
        progress aria-label="Reading"
      if failed(thermal)
        text "Couldn't read the thermal state."
      text thermal.state testId="thermal"
      button press=check testId="check"
        text "Check again"
```

```ts
import type { Answer, Result, Sources } from './app.contract.d.ts';

export const grants = '';
const sources: Sources = {
  thermalState: async (_args, _store, _storage, native) =>
    native?.available ? await native.later({}) as Result<'thermalState'> : { state: 'unknown' },
};
export const answer: Answer = (source, args, store, storage, native) =>
  sources[source](args, store, storage, native);
```

`call` runs within 100 ms (main on Apple, caller thread on Android); throw to refuse. `later` starts on main; answer once via `reply.send` or `reply.fail` from any thread. Rebuild with `bun exact.mjs ios` or `android`.

For `API_BASE_URL`, set `launch: true` in manifest `env` and read `import.meta.env.API_BASE_URL` (`SIMCTL_CHILD_API_BASE_URL` on iOS simulator; Android/web use build value).
