tauri_plugin_mobile_sharetarget/
lib.rs

1use tauri::{
2    plugin::{Builder, TauriPlugin},
3    Manager, Runtime,
4};
5
6#[cfg(desktop)]
7mod desktop;
8#[cfg(mobile)]
9mod mobile;
10
11mod commands;
12mod error;
13mod intents;
14
15pub use error::{Error, Result};
16
17#[cfg(desktop)]
18use desktop::MobileSharetarget;
19#[cfg(mobile)]
20use mobile::MobileSharetarget;
21
22pub use crate::intents::push_new_intent;
23
24/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the mobile-sharetarget APIs.
25pub trait MobileSharetargetExt<R: Runtime> {
26    fn mobile_sharetarget(&self) -> &MobileSharetarget<R>;
27}
28
29impl<R: Runtime, T: Manager<R>> crate::MobileSharetargetExt<R> for T {
30    fn mobile_sharetarget(&self) -> &MobileSharetarget<R> {
31        self.state::<MobileSharetarget<R>>().inner()
32    }
33}
34
35use std::sync::OnceLock;
36pub static IOS_DEEP_LINK_SCHEME: OnceLock<String> = OnceLock::new();
37
38/// Initializes the plugin.
39pub fn init<R: Runtime>() -> TauriPlugin<R> {
40    Builder::new("mobile-sharetarget")
41        .invoke_handler(tauri::generate_handler![
42            commands::pop_intent_queue,
43            commands::pop_intent_queue_and_extract_text
44        ])
45        .setup(|app, api| {
46            #[cfg(target_os = "ios")]
47            IOS_DEEP_LINK_SCHEME
48                .set(
49                    extract_deep_link_scheme(&app.config().plugins.0)
50                        .expect("Deeplink plugin hasn't been installed or properly configured"),
51                )
52                .expect("Once Lock already set");
53            #[cfg(mobile)]
54            let mobile_sharetarget = mobile::init(app, api)?;
55            #[cfg(desktop)]
56            let mobile_sharetarget = desktop::init(app, api)?;
57            app.manage(mobile_sharetarget);
58            Ok(())
59        })
60        .build()
61}
62
63#[cfg(target_os = "android")]
64use jni::{
65    objects::{JClass, JString},
66    JNIEnv,
67};
68
69#[cfg(target_os = "android")]
70#[no_mangle]
71pub extern "system" fn Java_com_plugin_mobilesharetarget_Sharetarget_pushIntent(
72    mut env: JNIEnv,
73    _class: JClass,
74    intent: JString,
75) {
76    println!("Calling JNI Hello World!");
77
78    let input: String = env
79        .get_string(&intent)
80        .expect("Couldn't get java string!")
81        .into();
82
83    push_new_intent(input);
84}
85
86#[cfg(target_os = "ios")]
87use anyhow::anyhow;
88#[cfg(target_os = "ios")]
89use serde_json::Value;
90#[cfg(target_os = "ios")]
91use std::collections::HashMap;
92#[cfg(target_os = "ios")]
93fn extract_deep_link_scheme(plugins_value: &HashMap<String, Value>) -> Result<String> {
94    // 1. Get the "deep-link" object
95    let deep_link = plugins_value
96        .get("deep-link")
97        .ok_or(anyhow!("Error: Missing 'deep-link' configuration block."))?;
98
99    // 2. Get the "mobile" array
100    let mobile_array = deep_link
101        .get("mobile")
102        .ok_or(anyhow!("Error: Missing 'mobile' key within 'deep-link'."))?
103        .as_array()
104        .ok_or(anyhow!("Error: 'mobile' value is not a JSON array."))?;
105
106    // 3. Get the first element [0] of the mobile array (the object)
107    let first_mobile_obj = mobile_array
108        .get(0)
109        .ok_or(anyhow!("Error: 'mobile' array is empty."))?;
110
111    // 4. Get the "scheme" array from the first mobile object
112    let scheme_array = first_mobile_obj
113        .get("scheme")
114        .ok_or(anyhow!(
115            "Error: Missing 'scheme' key in mobile configuration."
116        ))?
117        .as_array()
118        .ok_or(anyhow!("Error: 'scheme' value is not a JSON array."))?;
119
120    // 5. Get the first element [0] of the scheme array (the string value)
121    let first_scheme_value = scheme_array
122        .get(0)
123        .ok_or(anyhow!("Error: 'scheme' array is empty."))?
124        .as_str()
125        .ok_or(anyhow!("Error: Scheme value is not a string."))?;
126
127    // Return the extracted string value
128    Ok(first_scheme_value.to_string())
129}