@capgo/capacitor-share-target 8.0.5 → 8.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -42,8 +42,67 @@ For iOS, you need to create a Share Extension to receive shared content. This re
42
42
  1. In Xcode, go to File > New > Target
43
43
  2. Select "Share Extension" and click Next
44
44
  3. Name it (e.g., "ShareExtension") and click Finish
45
- 4. Configure the Share Extension to save data to a shared App Group
46
- 5. Update the `YOUR_APP_GROUP_ID` in the iOS plugin code
45
+ 4. Configure the Share Extension to save data to a shared App Group (e.g., `group.com.yourcompany.app`)
46
+ 5. Configure the plugin in your `capacitor.config.ts` or `capacitor.config.json`:
47
+
48
+ ```typescript
49
+ {
50
+ plugins: {
51
+ CapacitorShareTarget: {
52
+ appGroupId: "group.com.yourcompany.app"
53
+ }
54
+ }
55
+ }
56
+ ```
57
+
58
+ 6. In your ShareViewController.swift, save the shared data using the key `"share-target-data"` or `"SharedData"` to UserDefaults with your app group ID
59
+
60
+ Example ShareViewController.swift:
61
+
62
+ ```swift
63
+ import UIKit
64
+ import UniformTypeIdentifiers
65
+
66
+ class ShareViewController: UIViewController {
67
+ let APP_GROUP_ID = "group.com.yourcompany.app" // Same as in capacitor.config
68
+ let APP_URL_SCHEME = "yourapp" // Your app's URL scheme
69
+
70
+ private var texts: [[String: Any]] = []
71
+ private var files: [[String: Any]] = []
72
+
73
+ override public func viewDidLoad() {
74
+ super.viewDidLoad()
75
+
76
+ guard let extensionItem = extensionContext?.inputItems.first as? NSExtensionItem,
77
+ let attachments = extensionItem.attachments else {
78
+ self.exit()
79
+ return
80
+ }
81
+
82
+ Task {
83
+ // Process attachments...
84
+ // (See full implementation in issue examples)
85
+
86
+ // Save to UserDefaults
87
+ let shareData: [String: Any] = [
88
+ "title": extensionItem.attributedTitle?.string ?? "",
89
+ "texts": texts,
90
+ "files": files
91
+ ]
92
+
93
+ let userDefaults = UserDefaults(suiteName: APP_GROUP_ID)
94
+ userDefaults?.set(shareData, forKey: "share-target-data")
95
+ userDefaults?.synchronize()
96
+
97
+ // Open main app with your URL scheme
98
+ let url = URL(string: "\(APP_URL_SCHEME)://share")!
99
+ self.openURL(url)
100
+ }
101
+ }
102
+
103
+ // ... rest of implementation
104
+ }
105
+ ```
47
106
 
48
107
  For detailed instructions, see the [iOS Share Extension documentation](https://developer.apple.com/documentation/uikit/uiactivityviewcontroller).
49
108
 
@@ -7,7 +7,7 @@ import Capacitor
7
7
  */
8
8
  @objc(CapacitorShareTargetPlugin)
9
9
  public class CapacitorShareTargetPlugin: CAPPlugin, CAPBridgedPlugin {
10
- private let pluginVersion: String = "8.0.5"
10
+ private let pluginVersion: String = "8.0.6"
11
11
  public let identifier = "CapacitorShareTargetPlugin"
12
12
  public let jsName = "CapacitorShareTarget"
13
13
  public let pluginMethods: [CAPPluginMethod] = [
@@ -40,42 +40,73 @@ public class CapacitorShareTargetPlugin: CAPPlugin, CAPBridgedPlugin {
40
40
  }
41
41
 
42
42
  // Handle share extension URL scheme
43
- if url.scheme == "capacitor" && url.host == "share" {
43
+ // Support both "capacitor://share" and custom schemes with "/share" or "://share" paths
44
+ if (url.scheme == "capacitor" && url.host == "share") || url.host == "share" || url.path == "/share" {
44
45
  checkForSharedContent()
45
46
  }
46
47
  }
47
48
 
48
49
  private func checkForSharedContent() {
50
+ // Get app group ID from configuration or use placeholder
51
+ let appGroupId = getConfigValue("appGroupId") as? String ?? "group.YOUR_APP_GROUP_ID"
52
+
53
+ // Warn if placeholder is still being used
54
+ if appGroupId == "group.YOUR_APP_GROUP_ID" {
55
+ CAPLog.print("⚠️ ShareTarget: Using placeholder app group ID. Please configure 'appGroupId' in capacitor.config to receive share events.")
56
+ }
57
+
49
58
  // Get shared content from UserDefaults (set by share extension)
50
- if let userDefaults = UserDefaults(suiteName: "group.YOUR_APP_GROUP_ID") {
51
- if let sharedData = userDefaults.dictionary(forKey: "SharedData") {
52
- var shareEvent: [String: Any] = [:]
53
-
54
- // Get title
55
- shareEvent["title"] = sharedData["title"] as? String ?? ""
56
-
57
- // Get texts
58
- var texts: [String] = []
59
- if let sharedTexts = sharedData["texts"] as? [String] {
60
- texts = sharedTexts
61
- }
62
- shareEvent["texts"] = texts
63
-
64
- // Get files
65
- var files: [[String: Any]] = []
66
- if let sharedFiles = sharedData["files"] as? [[String: Any]] {
67
- files = sharedFiles
68
- }
69
- shareEvent["files"] = files
70
-
71
- // Clear the shared data
72
- userDefaults.removeObject(forKey: "SharedData")
73
- userDefaults.synchronize()
74
-
75
- // Notify listeners
76
- notifyListeners("shareReceived", data: shareEvent)
59
+ guard let userDefaults = UserDefaults(suiteName: appGroupId) else {
60
+ return
61
+ }
62
+
63
+ // Try multiple possible keys for compatibility
64
+ // Users may use "share-target-data" or "SharedData"
65
+ let possibleKeys = ["share-target-data", "SharedData"]
66
+ var sharedData: [String: Any]?
67
+ var usedKey: String?
68
+
69
+ for key in possibleKeys {
70
+ if let data = userDefaults.dictionary(forKey: key) {
71
+ sharedData = data
72
+ usedKey = key
73
+ break
77
74
  }
78
75
  }
76
+
77
+ guard let data = sharedData, let key = usedKey else {
78
+ return
79
+ }
80
+
81
+ // Parse shared data
82
+ var shareEvent: [String: Any] = [:]
83
+
84
+ // Get title
85
+ shareEvent["title"] = data["title"] as? String ?? ""
86
+
87
+ // Get texts - handle both array of strings and array of dictionaries
88
+ var texts: [String] = []
89
+ if let sharedTexts = data["texts"] as? [String] {
90
+ texts = sharedTexts
91
+ } else if let sharedTexts = data["texts"] as? [[String: Any]] {
92
+ // Handle case where texts is array of dictionaries with "value" key
93
+ texts = sharedTexts.compactMap { $0["value"] as? String }
94
+ }
95
+ shareEvent["texts"] = texts
96
+
97
+ // Get files
98
+ var files: [[String: Any]] = []
99
+ if let sharedFiles = data["files"] as? [[String: Any]] {
100
+ files = sharedFiles
101
+ }
102
+ shareEvent["files"] = files
103
+
104
+ // Clear the shared data
105
+ userDefaults.removeObject(forKey: key)
106
+ userDefaults.synchronize()
107
+
108
+ // Notify listeners
109
+ notifyListeners("shareReceived", data: shareEvent)
79
110
  }
80
111
 
81
112
  @objc func getPluginVersion(_ call: CAPPluginCall) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-share-target",
3
- "version": "8.0.5",
3
+ "version": "8.0.6",
4
4
  "description": "Receive shared content from other apps",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",