diff --git a/share/README.md b/share/README.md index cbd04bc5f..06fb7ae11 100644 --- a/share/README.md +++ b/share/README.md @@ -31,6 +31,7 @@ Each platform uses a different set of fields, but you should supply them all. +* [`canShare()`](#canshare) * [`share(...)`](#share) * [Interfaces](#interfaces) @@ -39,6 +40,21 @@ Each platform uses a different set of fields, but you should supply them all. +### canShare() + +```typescript +canShare() => Promise +``` + +Check if sharing is supported. + +**Returns:** Promise<CanShareResult> + +**Since:** 1.1.0 + +-------------------- + + ### share(...) ```typescript @@ -61,6 +77,13 @@ Show a Share modal for sharing content with other apps ### Interfaces +#### CanShareResult + +| Prop | Type | Description | Since | +| ----------- | -------------------- | ------------------------------------ | ----- | +| **`value`** | boolean | Whether sharing is supported or not. | 1.1.0 | + + #### ShareResult | Prop | Type | Description | Since | diff --git a/share/android/src/main/java/com/capacitorjs/plugins/share/SharePlugin.java b/share/android/src/main/java/com/capacitorjs/plugins/share/SharePlugin.java index 23efd2961..edef5ff2d 100644 --- a/share/android/src/main/java/com/capacitorjs/plugins/share/SharePlugin.java +++ b/share/android/src/main/java/com/capacitorjs/plugins/share/SharePlugin.java @@ -50,6 +50,13 @@ private void activityResult(PluginCall call, ActivityResult result) { isPresenting = false; } + @PluginMethod + public void canShare(PluginCall call) { + JSObject callResult = new JSObject(); + callResult.put("value", true); + call.resolve(callResult); + } + @PluginMethod public void share(PluginCall call) { if (!isPresenting) { diff --git a/share/ios/Plugin/SharePlugin.m b/share/ios/Plugin/SharePlugin.m index 4ead81c1d..42cde0f51 100644 --- a/share/ios/Plugin/SharePlugin.m +++ b/share/ios/Plugin/SharePlugin.m @@ -4,5 +4,6 @@ // Define the plugin using the CAP_PLUGIN Macro, and // each method the plugin supports using the CAP_PLUGIN_METHOD macro. CAP_PLUGIN(SharePlugin, "Share", + CAP_PLUGIN_METHOD(canShare, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(share, CAPPluginReturnPromise); ) diff --git a/share/ios/Plugin/SharePlugin.swift b/share/ios/Plugin/SharePlugin.swift index e2c767ff5..ff9950ab9 100644 --- a/share/ios/Plugin/SharePlugin.swift +++ b/share/ios/Plugin/SharePlugin.swift @@ -4,6 +4,12 @@ import Capacitor @objc(SharePlugin) public class SharePlugin: CAPPlugin { + @objc func canShare(_ call: CAPPluginCall) { + call.resolve([ + "value": true + ]) + } + @objc func share(_ call: CAPPluginCall) { var items = [Any]() diff --git a/share/src/definitions.ts b/share/src/definitions.ts index 730e55676..d616d06b9 100644 --- a/share/src/definitions.ts +++ b/share/src/definitions.ts @@ -42,7 +42,23 @@ export interface ShareResult { activityType?: string; } +export interface CanShareResult { + /** + * Whether sharing is supported or not. + * + * @since 1.1.0 + */ + value: boolean; +} + export interface SharePlugin { + /** + * Check if sharing is supported. + * + * @since 1.1.0 + */ + canShare(): Promise; + /** * Show a Share modal for sharing content with other apps * diff --git a/share/src/web.ts b/share/src/web.ts index e38298743..ccd0d4c56 100644 --- a/share/src/web.ts +++ b/share/src/web.ts @@ -1,8 +1,20 @@ import { WebPlugin } from '@capacitor/core'; -import type { ShareOptions, SharePlugin, ShareResult } from './definitions'; +import type { + CanShareResult, + ShareOptions, + SharePlugin, + ShareResult, +} from './definitions'; export class ShareWeb extends WebPlugin implements SharePlugin { + async canShare(): Promise { + if (typeof navigator === 'undefined' || !navigator.share) { + return { value: false }; + } else { + return { value: true }; + } + } async share(options: ShareOptions): Promise { if (typeof navigator === 'undefined' || !navigator.share) { throw this.unavailable('Share API not available in this browser');