npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rea.ch/capacitor-native-share

v5.0.5

Published

Converts a native share to an event

Downloads

144

Readme

@rea.ch/capacitor-native-share

Converts a native share to a js event.

Install

In order to use this library, first add it to your project by:

npm install @rea.ch/capacitor-native-share --save

or

yarn add @rea.ch/capacitor-native-share

Web

There are two methods that can be used to obtain the shared items:

  • getLastSharedItems: Returns a promise that resolves in the last items shared to the app. The idea behind this is to use it as soon as the app is initialized.

  • addListener: Emits every time the app receives a share only if the listener was registered before receiving the shared.

Example

import { NativeShare } from '@rea.ch/capacitor-native-share';

// ...

public onAppInitialization(): void {
	NativeShare.getLastSharedItems()
		.then(this.handleShare.bind(this))
		.catch(console.error);

	NativeShare.addListener(
		NativeShareEventType.SHARE_RECEIVED,
		this.handleShare.bind(this)
	);
}

private handleShare(share: NativeShareShareReceived): void {
	// ...
}

Android

Edit your android's AndroidManifest.xml file with the types that you want to handle (ref):

<!-- Single share -->
<intent-filter>
	<action android:name="android.intent.action.SEND" />

	<category android:name="android.intent.category.DEFAULT" />
	<category android:name="android.intent.category.BROWSABLE" />

	<!-- Any type of data -->
	<data android:mimeType="*/*" />
</intent-filter>

<!-- multiple share -->
<intent-filter>
	<action android:name="android.intent.action.SEND_MULTIPLE" />

	<category android:name="android.intent.category.DEFAULT" />
	<category android:name="android.intent.category.BROWSABLE" />

	<!-- Any type of data -->
	<data android:mimeType="*/*" />
</intent-filter>

iOS

There are some special setup that needs to be done in iOS:

App Extension

Create an App Share Extension.

App Group

Create an App Group so the extension and the app can share data.

Modify your Podfile

You need to modify your Podfile to include a library to your extension:

...
target 'App' do
  capacitor_pods
  # Add your Pods here
end

+ target 'YOUR_SHARE_EXTENSION_NAME' do
+   # We need this to override the extension class
+   pod 'Rea.chCapacitorNativeShare', :path => '../../node_modules/@rea.ch/capacitor-share-extension'
+ end

Override extension class

Extend the class NativeShareExtension in your extension's ShareViewController.swift and:

  1. override the function getContainerAppUrlExtension so it returns your App's Url Scheme (tutorial).

  2. override the function getAppGroupIdentifier so it returns yout extension and app's App Group's name.

import UIKit
import Social
import MobileCoreServices
import ReachCapacitorNativeShare

class ShareViewController: NativeShareExtension {
    override func getContainerAppUrlExtension() -> String {
        return "ReachCapacitorNativeShareExample"
    }

    override func getAppGroupIdentifier() -> String {
        return "group.ch.rea.plugins.nativeshareexample"
    }
}

Override AppDelegate class

Modify your AppDelegate.swift with the following

import ReachCapacitorNativeShare

// ...

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    // ...
    
    var store: NativeShareStore = NativeShareStore.store

	// ...

	func applicationWillTerminate(_ application: UIApplication) {
        NativeShareDelegateUtils.cleanTemporalFolder( appGroupName: "YOUR_APP_GROUP_NAME" )
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        let success: Bool = ApplicationDelegateProxy.shared.application(app, open: url, options: options)

        let nativeShareHandlerSuccess = NativeShareDelegateUtils.handleApplicationUrl(app: app, url: url, store: store)

        return success && nativeShareHandlerSuccess
    }

	// ...
}

Common errors

sharedApplication is unavailable

'sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based solutions where appropriate instead.

Uncheck the Pods targets Capacitor and CapacitorCordova's Allow app extension API only option after every cap sync or pod install. ref

API

Interfaces

NativeShareGetItemsOptions

| Prop | Type | Description | | ---------------- | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | | autoRemove | boolean | Whether to remove the share event so if you call {@link NativeSharePlugin.getLastSharedItems} again it will return void. Default: true. |

NativeShareShareReceived

| Prop | Type | | ------------ | ------------------------------------------------------------------------------- | | items | { [idx: string]: NativeShareItem; } | | length | number |

NativeShareItem

| Prop | Type | Description | | -------------- | ------------------- | ------------------------------------------ | | text | string | The text shared. It can also be a website. | | uri | string | The uri (path) to the shared file. | | mimeType | string | The mimeType of the shared file. |

Alternatives

This library is based on these two libraries that handles the share in a similar way: