Skip to content

Commit

Permalink
feat(share): Add canShare method to check availability (#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Jan 7, 2022
1 parent af4b875 commit 3883d82
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
23 changes: 23 additions & 0 deletions share/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Each platform uses a different set of fields, but you should supply them all.

<docgen-index>

* [`canShare()`](#canshare)
* [`share(...)`](#share)
* [Interfaces](#interfaces)

Expand All @@ -39,6 +40,21 @@ Each platform uses a different set of fields, but you should supply them all.
<docgen-api>
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->

### canShare()

```typescript
canShare() => Promise<CanShareResult>
```

Check if sharing is supported.

**Returns:** <code>Promise&lt;<a href="#canshareresult">CanShareResult</a>&gt;</code>

**Since:** 1.1.0

--------------------


### share(...)

```typescript
Expand All @@ -61,6 +77,13 @@ Show a Share modal for sharing content with other apps
### Interfaces


#### CanShareResult

| Prop | Type | Description | Since |
| ----------- | -------------------- | ------------------------------------ | ----- |
| **`value`** | <code>boolean</code> | Whether sharing is supported or not. | 1.1.0 |


#### ShareResult

| Prop | Type | Description | Since |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions share/ios/Plugin/SharePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
)
6 changes: 6 additions & 0 deletions share/ios/Plugin/SharePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]()

Expand Down
16 changes: 16 additions & 0 deletions share/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CanShareResult>;

/**
* Show a Share modal for sharing content with other apps
*
Expand Down
14 changes: 13 additions & 1 deletion share/src/web.ts
Original file line number Diff line number Diff line change
@@ -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<CanShareResult> {
if (typeof navigator === 'undefined' || !navigator.share) {
return { value: false };
} else {
return { value: true };
}
}
async share(options: ShareOptions): Promise<ShareResult> {
if (typeof navigator === 'undefined' || !navigator.share) {
throw this.unavailable('Share API not available in this browser');
Expand Down

0 comments on commit 3883d82

Please sign in to comment.