How myreach-sa/capacitor-native-share works
Project type and purpose
• This is a Capacitor plugin (“@rea.ch/capacitor-native-share”) that exposes native Android and iOS share‐target functionality as JavaScript events.
• It provides a unified JS API (getLastSharedItems, addListener) and bridges to two platform‐specific modules plus a web fallback.
File structure
• Top-level:
• src/ – JS interface, web fallback, TypeScript definitions
• android/ – Android plugin module (Java code, Gradle files, intent filters)
• ios/ – Xcode project for the plugin, Swift/Obj-C bridges, share extension helpers
• example/ – A sample Capacitor/Ionic app demonstrating integration on Android and iOS
• rollup.config.js, package.json – build & packaging of the plugin
• Patterns:
• Bridge pattern: JS → Capacitor bridge → native module
• Plugin/extension pattern on iOS: host app + share extension + app group storage
Key insights from README
• JS side: exposes two methods and an event type
• Android: relies on intent-filters in AndroidManifest.xml
• iOS: uses an App Extension, App Group, URL schemes, and delegates in AppDelegate
Main components & their interactions
a. Components
1. Host Application (Ionic/Capacitor app)
2. Plugin JS Layer (src/index.ts, src/web.ts)
3. Capacitor Runtime/Bridge
4. Android Native Plugin (android/src/main/java/.../NativeSharePlugin.java)
5. iOS Native Plugin (ios/Plugin/NativeSharePlugin.swift)
6. iOS Share Extension (ios/Plugin/NativeShareExtension.swift subclass)
7. Shared Data Store (iOS App Group container; Android temporary storage via Intent extras)
8. Example App (example/native-share-example)
b. Relationships & data flows
• At runtime, the host app invokes NativeShare.getLastSharedItems() or registers NativeShare.addListener().
• The plugin JS calls Capacitor.Plugins.NativeShare.getLastSharedItems() → bridge → native module.
• Android:
– OS issues a SHARE intent → the plugin’s intent filter catches it → NativeSharePlugin reads Intent extras → passes data back to JS via the bridge.
• iOS:
– Share Extension intercepts share → writes shared items to the App Group container → invokes host via custom URL scheme → AppDelegate receives URL → NativeShareDelegateUtils.handleApplicationUrl loads items from the shared container → plugin dispatches JS event.
• Web: fallback in src/web.ts returns empty or dummy.
c. Architectural patterns & principles
• Cross‐platform plugin abstraction layer
• Bridge pattern courtesy of Capacitor runtime
• Separation of concerns: JS API vs. platform implementations vs. example app
• Modular design: Android and iOS code co-exist but remain platform-specific
d. Relevant technologies
• TypeScript, Rollup for bundling
• Capacitor plugin API
• Android intents, Gradle build system
• iOS App Extensions, App Groups, Swift/Obj-C bridging
• Example uses Angular/Ionic