Nucleus
OS integration

Share sheetNew

Open the system share sheet from Kotlin Multiplatform code, attached to a Nucleus window on desktop.

View as Markdown

share opens the system share sheet with text, links and files. It is a Kotlin Multiplatform module: the same call works on macOS, Windows, Linux, Android, iOS and the web.

sharemacOSWindowsLinuxAndroidiOSWebDesktop Gradle plugin: Not neededDesktop window runtime: Not needed
PlatformShare UI
macOSNSSharingServicePicker
WindowsShare UI (DataTransferManager)
LinuxXDG desktop portal: "Open With" for one file or URL, "Save files" for a mixed payload
AndroidAndroid Sharesheet
iOSUIActivityViewController
Web (js, wasmJs)Web Share API (navigator.share)

Add the dependency

build.gradle.kts
// commonMain in a KMP project, dependencies { } in a JVM desktop app
implementation("dev.nucleusframework:nucleus.share:3.0.0")

nucleus-application does not bring it: add it yourself to call NucleusWindow.share.

Share from a Nucleus window

val window = LocalNucleusWindow.current
val density = LocalDensity.current
val scope = rememberCoroutineScope()
var anchor by remember { mutableStateOf<ShareAnchor?>(null) }

Button(
    onClick = {
        scope.launch {
            window.share(anchor) {
                title = "Share"
                text("Cross-platform native share sheet")
                url("https://nucleusframework.dev")
                file("/path/to/report.pdf", mimeType = "application/pdf")
            }
        }
    },
    modifier = Modifier.onGloballyPositioned { anchor = it.shareAnchor(density) },
) { Text("Share") }

NucleusWindow.share attaches the sheet to the window: its HWND on Windows, its NSWindow on macOS, x11:<xid> on X11, and an xdg_foreign export on Wayland, released once the portal dialog closes. On macOS the picker points at anchor, or at the top centre of the window without one. A window that is not realized yet falls back to the app's frontmost window.

The macOS share sheet anchored to the button that opened it

Share from common code

scope.launch {
    try {
        ShareSheet.share {
            title = "Share"       // chooser title where the platform has one
            subject = "Nucleus"   // e-mail subject on Android, description on Windows
            text("Hello")
            url("https://nucleusframework.dev")
            fileUri("content://com.example/report.txt")
        }
    } catch (e: ShareException) {
        log("${e.error}: ${e.message}")
    }
}
  • share is suspend and safe from any dispatcher. It returns once the UI is on screen (once the request is dispatched on Android and Linux, once the sheet closes on the web).
  • It reports neither the target the user picked nor a cancel.
  • Build a request directly with ShareRequest(items, title, subject) or shareRequest { … }. Items are ShareItem.Text, Url, File(path, mimeType) and FileUri(uri, mimeType).
  • ShareSheet.isSupported tells whether a share UI is available: on desktop, whether the native bridge loaded.

On desktop without nucleus-application, name the parent yourself with ShareParent.Windows(hwnd), ShareParent.MacOs(nsWindow, anchor), ShareParent.Linux(portalParent, keepAlive) or ShareParent.Auto (the default). On Android, pass a Context: ShareSheet.share(request, activity).

Handle errors

ShareException.error is a ShareError. The request is validated in common code first.

ValueMeaning
EmptyNo item
InvalidItemBlank item, unparsable URL or blank MIME type
NoHandlerNo app can receive the payload
AlreadyOpenA share sheet is already on screen (iOS, web)
UnsupportedItemItem kind unsupported here: content:// on desktop, a local path on the web
NoWindowNo window or activity to present from
UnsupportedNo share UI, or the native bridge is missing
IoReading or staging a file failed
PlatformThe platform API failed; see the message

Platform notes

  • macOS: title and subject are ignored. Without a running NSApplication the call fails after 10 s instead of hanging.
  • Windows: texts and URLs are joined into the payload's text, and the first URL is also its web link. Files accept paths and file:// URIs.
  • Linux: there is no share sheet. One URL or file opens the portal's "Open With" chooser, one text goes through a temporary file, and a mixed payload opens a "Save files" dialog. Without a portal, Nucleus falls back to xdg-open, then fails with NoHandler. On native Wayland, ShareParent.Auto sends no parent: share from a window.
  • Android: local files are copied to the cache and served read-only by the library's own NucleusShareProvider (authority ${applicationId}.nucleus.share). content:// URIs are forwarded with a read grant. No init call is needed. minSdk is 21.
  • iOS: title and subject are ignored. On iPad the popover is centred on the presenting view controller.
  • Web: MIME types matter, as on Android.

Web: secure context and user gesture

The Web Share API needs HTTPS or localhost, and a user gesture: call share from a click handler without awaiting anything slow first, or the browser rejects it (ShareError.Platform). There is no filesystem, so file(path) fails: share fileUri("blob:…"), data: or same-origin https: URIs. Browsers without navigator.share, such as Firefox desktop, report isSupported = false.

What's next

Was this page helpful?
Edit on GitHub

Last updated on