feat: saveToken plugin for app groups on iOS
This will allow the share extension to access the Bearer token to send images to the backend.
This commit is contained in:
13
tauri-plugin-ios-shared-token/src/commands.rs
Normal file
13
tauri-plugin-ios-shared-token/src/commands.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use tauri::{AppHandle, command, Runtime};
|
||||
|
||||
use crate::models::*;
|
||||
use crate::Result;
|
||||
use crate::IosSharedTokenExt;
|
||||
|
||||
#[command]
|
||||
pub(crate) async fn ping<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
payload: PingRequest,
|
||||
) -> Result<PingResponse> {
|
||||
app.ios_shared_token().ping(payload)
|
||||
}
|
||||
22
tauri-plugin-ios-shared-token/src/desktop.rs
Normal file
22
tauri-plugin-ios-shared-token/src/desktop.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use serde::de::DeserializeOwned;
|
||||
use tauri::{plugin::PluginApi, AppHandle, Runtime};
|
||||
|
||||
use crate::models::*;
|
||||
|
||||
pub fn init<R: Runtime, C: DeserializeOwned>(
|
||||
app: &AppHandle<R>,
|
||||
_api: PluginApi<R, C>,
|
||||
) -> crate::Result<IosSharedToken<R>> {
|
||||
Ok(IosSharedToken(app.clone()))
|
||||
}
|
||||
|
||||
/// Access to the ios-shared-token APIs.
|
||||
pub struct IosSharedToken<R: Runtime>(AppHandle<R>);
|
||||
|
||||
impl<R: Runtime> IosSharedToken<R> {
|
||||
pub fn ping(&self, payload: PingRequest) -> crate::Result<PingResponse> {
|
||||
Ok(PingResponse {
|
||||
value: payload.value,
|
||||
})
|
||||
}
|
||||
}
|
||||
21
tauri-plugin-ios-shared-token/src/error.rs
Normal file
21
tauri-plugin-ios-shared-token/src/error.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use serde::{ser::Serializer, Serialize};
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error(transparent)]
|
||||
Io(#[from] std::io::Error),
|
||||
#[cfg(mobile)]
|
||||
#[error(transparent)]
|
||||
PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
|
||||
}
|
||||
|
||||
impl Serialize for Error {
|
||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.to_string().as_ref())
|
||||
}
|
||||
}
|
||||
48
tauri-plugin-ios-shared-token/src/lib.rs
Normal file
48
tauri-plugin-ios-shared-token/src/lib.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use tauri::{
|
||||
plugin::{Builder, TauriPlugin},
|
||||
Manager, Runtime,
|
||||
};
|
||||
|
||||
pub use models::*;
|
||||
|
||||
#[cfg(desktop)]
|
||||
mod desktop;
|
||||
#[cfg(mobile)]
|
||||
mod mobile;
|
||||
|
||||
mod commands;
|
||||
mod error;
|
||||
mod models;
|
||||
|
||||
pub use error::{Error, Result};
|
||||
|
||||
#[cfg(desktop)]
|
||||
use desktop::IosSharedToken;
|
||||
#[cfg(mobile)]
|
||||
use mobile::IosSharedToken;
|
||||
|
||||
/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the ios-shared-token APIs.
|
||||
pub trait IosSharedTokenExt<R: Runtime> {
|
||||
fn ios_shared_token(&self) -> &IosSharedToken<R>;
|
||||
}
|
||||
|
||||
impl<R: Runtime, T: Manager<R>> crate::IosSharedTokenExt<R> for T {
|
||||
fn ios_shared_token(&self) -> &IosSharedToken<R> {
|
||||
self.state::<IosSharedToken<R>>().inner()
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes the plugin.
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
Builder::new("ios-shared-token")
|
||||
.invoke_handler(tauri::generate_handler![commands::ping])
|
||||
.setup(|app, api| {
|
||||
#[cfg(mobile)]
|
||||
let ios_shared_token = mobile::init(app, api)?;
|
||||
#[cfg(desktop)]
|
||||
let ios_shared_token = desktop::init(app, api)?;
|
||||
app.manage(ios_shared_token);
|
||||
Ok(())
|
||||
})
|
||||
.build()
|
||||
}
|
||||
34
tauri-plugin-ios-shared-token/src/mobile.rs
Normal file
34
tauri-plugin-ios-shared-token/src/mobile.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use serde::de::DeserializeOwned;
|
||||
use tauri::{
|
||||
plugin::{PluginApi, PluginHandle},
|
||||
AppHandle, Runtime,
|
||||
};
|
||||
|
||||
use crate::models::*;
|
||||
|
||||
#[cfg(target_os = "ios")]
|
||||
tauri::ios_plugin_binding!(init_plugin_ios_shared_token);
|
||||
|
||||
// initializes the Kotlin or Swift plugin classes
|
||||
pub fn init<R: Runtime, C: DeserializeOwned>(
|
||||
_app: &AppHandle<R>,
|
||||
api: PluginApi<R, C>,
|
||||
) -> crate::Result<IosSharedToken<R>> {
|
||||
#[cfg(target_os = "android")]
|
||||
let handle = api.register_android_plugin("", "ExamplePlugin")?;
|
||||
#[cfg(target_os = "ios")]
|
||||
let handle = api.register_ios_plugin(init_plugin_ios_shared_token)?;
|
||||
Ok(IosSharedToken(handle))
|
||||
}
|
||||
|
||||
/// Access to the ios-shared-token APIs.
|
||||
pub struct IosSharedToken<R: Runtime>(PluginHandle<R>);
|
||||
|
||||
impl<R: Runtime> IosSharedToken<R> {
|
||||
pub fn ping(&self, payload: PingRequest) -> crate::Result<PingResponse> {
|
||||
self
|
||||
.0
|
||||
.run_mobile_plugin("ping", payload)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
13
tauri-plugin-ios-shared-token/src/models.rs
Normal file
13
tauri-plugin-ios-shared-token/src/models.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PingRequest {
|
||||
pub value: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PingResponse {
|
||||
pub value: Option<String>,
|
||||
}
|
||||
Reference in New Issue
Block a user