# Mobile SDKs

Source: https://mroffice.ai/en/developers/embed/mobile

Thin native wrappers host the same editor in a WebView, with the same token flow and a bridge for its events and commands: a Swift package (WKWebView, UIKit and SwiftUI), an Android library (Kotlin, WebView), a React Native component (react-native-webview) and a Flutter package (webview_flutter). Each is version 1.0.0 and has its own README and example.

- The wrapper loads one bundled page (the SDK and the bridge, inline) with a base URL you choose, hostOrigin: put that origin in the app's allowed origins (your https origin in production; any http://localhost port while you build, with Allow localhost on). The editor's frame then accepts the page as it accepts your website.
- Your app gives a token provider: an async function the wrapper calls with { reason, mode } whenever the editor needs a token. It asks your server, which signs the token as for the web; the secret key never ships in the app.
- Events (ready, change, save, error, saveRequested, modeChange, download, connectionChange, usersChange, mention, …) come to a delegate, callback or stream; commands (requestSave, setMode, setTheme, setLocale, focus, openFile, destroy) return the editor's answer.
- The bridge is locked to the page: every message carries a random nonce the editor's frame cannot read; iOS takes messages from the main frame only, Android uses WebViewCompat.addWebMessageListener for the host origin only (never addJavascriptInterface). Downloads come to your app as bytes (downloads: "host"); links open in the system browser.

| Platform | Package | Verified |
| --- | --- | --- |
| iOS 16.4+ (the editor's WebKit floor) | Swift package MROfficeEmbed: MROfficeEditorView (UIKit), MROfficeEditor (SwiftUI) | Built with Xcode; unit tests; run in the iOS Simulator against MR Office |
| Android 7+ (API 24) | Library ai.mroffice.embed: MROfficeEditorView (Kotlin) | Source and README (build with your Gradle) |
| React Native | mroffice-embed-react-native: <MROfficeEditor ref=… /> | Type-checked |
| Flutter | mroffice_embed: MROfficeEditor widget + MROfficeEditorController | Source and README (build with your Flutter) |

iOS (SwiftUI):

```swift
// Package.swift: .package(url: "https://github.com/…/MROfficeEmbed", from: "1.0.0")
import SwiftUI
import MROfficeEmbed

struct ContractView: View {
    @StateObject private var editor = MROfficeEditorController()

    var body: some View {
        MROfficeEditor(
            configuration: .init(
                appId: "YOUR_APP_ID",
                hostOrigin: URL(string: "https://app.example.com")!,   // one of the app's allowed origins
                locale: "en"
            ),
            tokenProvider: { request in
                // your server signs the token; the secret key never ships in the app
                try await API.editorToken(contract: "42", mode: request.mode ?? "edit")
            },
            controller: editor,
            onEvent: { event in if event.name == .save { print(event.payload) } }
        )
        .toolbar { Button("Save") { Task { try? await editor.requestSave() } } }
    }
}
```

Android (Kotlin):

```kotlin
// build.gradle.kts: implementation(project(":mroffice-embed"))  (or the published artifact)
val editor = MROfficeEditorView(context)
setContentView(editor)
editor.start(
    MROfficeEditorOptions(
        appId = "YOUR_APP_ID",
        hostOrigin = "https://app.example.com",          // one of the app's allowed origins
        locale = "en",
    ),
    tokenProvider = { request -> api.editorToken(contract = "42", mode = request.mode ?: "edit") },
    listener = { event -> if (event.name == "save") Log.i("editor", event.payload.toString()) },
)
saveButton.setOnClickListener { editor.requestSave { result -> /* … */ } }
```

React Native:

```tsx
// npm install mroffice-embed-react-native react-native-webview
import { useRef } from "react";
import { Button } from "react-native";
import { MROfficeEditor, type MROfficeEditorHandle } from "mroffice-embed-react-native";

export function Contract() {
  const editor = useRef<MROfficeEditorHandle>(null);
  return (
    <>
      <Button title="Save" onPress={() => editor.current?.requestSave()} />
      <MROfficeEditor
        ref={editor}
        appId="YOUR_APP_ID"
        hostOrigin="https://app.example.com"            // one of the app's allowed origins
        tokenProvider={({ mode }) => api.editorToken("42", mode ?? "edit")}
        onEvent={(name, payload) => name === "save" && console.log(payload)}
        style={{ flex: 1 }}
      />
    </>
  );
}
```

Flutter:

```dart
// pubspec.yaml: mroffice_embed: ^1.0.0 (depends on webview_flutter)
import 'package:mroffice_embed/mroffice_embed.dart';

final controller = MROfficeEditorController();

MROfficeEditor(
  appId: 'YOUR_APP_ID',
  hostOrigin: 'https://app.example.com',               // one of the app's allowed origins
  tokenProvider: (request) => api.editorToken('42', request.mode ?? 'edit'),
  controller: controller,
  onEvent: (event) { if (event.name == 'save') print(event.payload); },
);

await controller.requestSave();
```

### The editor on phones and in WebViews

- On a touch screen the editor uses touch-sized controls (its touch mode), fits the document to the width, and scrolls the ribbon sideways; ui.toolbar: "none" leaves the whole screen to the document.
- In your own mobile web page, give the page a viewport (<meta name="viewport" content="width=device-width, initial-scale=1">) and the editor's container the visible height (100dvh): the soft keyboard then resizes it instead of covering it. The wrappers' page does this for you, and keeps the page from zooming on a field's focus.
- ui.theme.density: "comfortable" turns touch-sized controls on everywhere, "compact" off everywhere (Pro and above).
