Zum Inhalt springen

Mobile API

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Native mobile capabilities are exposed two ways:

  • Per-platform managersapplication.IOS (in //go:build ios files) and application.Android (in //go:build android files). Use these for anything platform-specific. See the iOS and Android references for the full per-platform surface.
  • application.Mobile — a single, build-guarded manager covering the subset of capabilities that behave identically on both platforms. Use this when you want one code path that compiles and runs everywhere.

application.Mobile dispatches to IOS on iOS, to Android on Android, and to a no-op stub on desktop. Because it has no build constraint, you can call it from ordinary, platform-agnostic Go — no //go:build files of your own:

// Works in any file, on any target.
// On desktop this returns "" (no-op); on device it returns the real path.
dbDir := application.Mobile.StoragePath()
if dbDir == "" {
// Off-device, or the directory could not be created — handle accordingly.
return
}
db, _ := sql.Open("sqlite", filepath.Join(dbDir, "app.db"))

StoragePath() returns the absolute path to the app’s private files directory — getFilesDir() on Android, the Application Support directory on iOS — the recommended home for databases and other persistent files. It returns an empty string on desktop, and on device if the directory is unavailable (on iOS, if it cannot be created), so check for "" before using it.

The Mobile manager exposes the capabilities whose signatures are identical on iOS and Android:

CapabilityAPINotes
Share sheetMobile.Share(json){text, url}
Open URL externallyMobile.OpenURL(url)System browser
Keep screen awakeMobile.SetKeepAwake(bool)
Torch / flashlightMobile.SetTorch(bool)common:torch
Safe-area insetsMobile.SafeAreaJSON(){top,bottom,left,right}
App infoMobile.AppInfoJSON(){name,version,build,bundleId}
Orientation lockMobile.SetOrientation(mode)portrait / landscape / auto
Status barMobile.SetStatusBar(json)style + visibility
Storage infoMobile.StorageJSON(){free,total} bytes
Storage pathMobile.StoragePath()App-private files directory
Power / batteryMobile.PowerJSON(){level,charging,lowPower}
Network statusMobile.NetworkJSON(){connected,type}
BiometricsMobile.BiometricAuthenticate(reason)common:biometric
Secure storageMobile.SecureGet(key) / Mobile.SecureDelete(key)Keychain / EncryptedSharedPreferences
GeolocationMobile.GetLocation()one-shot → common:location
HapticsMobile.Haptic(type)impact / notification / selection
AccelerometerMobile.SetMotion(bool)common:motion
ProximityMobile.SetProximity(bool)common:proximity
Text-to-speechMobile.Speak(text) / Mobile.StopSpeak()
Keyboard insetsMobile.SetKeyboardWatch(bool)common:keyboard
Screen-captureMobile.SetScreenProtect(bool)common:screenCapture
CameraMobile.CapturePhoto() / Mobile.CaptureVideo()common:capture

Asynchronous results arrive as common:* events, exactly as with the per-platform managers — see Events for the payloads.

Capabilities that differ in shape between iOS and Android are not on Mobile; call them through application.IOS / application.Android from a build-tagged file:

ConcerniOSAndroid
Brightness (set)IOS.SetBrightness(0.0-1.0)Android.SetBrightness(0-100)
Brightness / orientation (get)IOS.GetBrightness() / IOS.GetOrientation()Android.BrightnessJSON() / Android.OrientationJSON()
Local notificationIOS.PostNotification(json)Android.Notify(json)
Secure store (write)IOS.SecureSet(key, value)Android.SecureSet(json)
Background executionIOS.BeginBackgroundTask / EndBackgroundTaskAndroid.StartForegroundService / StopForegroundService