mobile.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use serde::de::DeserializeOwned;
  5. use tauri::{
  6. plugin::{PluginApi, PluginHandle},
  7. AppHandle, Runtime,
  8. };
  9. use crate::models::*;
  10. #[cfg(target_os = "android")]
  11. const PLUGIN_IDENTIFIER: &str = "com.plugin.sample";
  12. #[cfg(target_os = "ios")]
  13. tauri::ios_plugin_binding!(init_plugin_sample);
  14. // initializes the Kotlin or Swift plugin classes
  15. pub fn init<R: Runtime, C: DeserializeOwned>(
  16. _app: &AppHandle<R>,
  17. api: PluginApi<R, C>,
  18. ) -> crate::Result<Sample<R>> {
  19. #[cfg(target_os = "android")]
  20. let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "ExamplePlugin")?;
  21. #[cfg(target_os = "ios")]
  22. let handle = api.register_ios_plugin(init_plugin_sample)?;
  23. Ok(Sample(handle))
  24. }
  25. /// A helper class to access the sample APIs.
  26. pub struct Sample<R: Runtime>(PluginHandle<R>);
  27. impl<R: Runtime> Sample<R> {
  28. pub fn ping(&self, payload: PingRequest) -> crate::Result<PingResponse> {
  29. self
  30. .0
  31. .run_mobile_plugin("ping", payload)
  32. .map_err(Into::into)
  33. }
  34. }