plugins icon indicating copy to clipboard operation
plugins copied to clipboard

[@nativescript/social-share] how to identify, shared with which app

Open PiyushHM opened this issue 5 years ago • 4 comments

For analytics purpose, need to know, with which app the contents are shared.

PiyushHM avatar Dec 29 '20 05:12 PiyushHM

Note: A Promise could be used in implementation to return anything the platform provides on selection.

NathanWalker avatar Jan 23 '21 18:01 NathanWalker

hi @NathanWalker

I tried workaround to achieve this using native code.

For iOS, it works with following code ...

let items = NSArray.arrayWithArray(url); let activity = UIActivityViewController.alloc().initWithActivityItemsApplicationActivities( items, null ); activity.completionWithItemsHandler = ( activityType, completed, returnedItems, error ) => { console.log("Here is activity Type" + activityType + completed); }; let rvc = UIApplication.sharedApplication.keyWindow.rootViewController; if (rvc) { rvc.presentViewControllerAnimatedCompletion(activity, true, null); }

Here 'activityType' and 'completed' provides information about package name and shared details.

But for Android, the pendingIntent is not returning package name with below code. Not sure if I am missing anything here.

let sharingIntent = new android.content.Intent( android.content.Intent.ACTION_SEND ); sharingIntent.setType("text/plain"); sharingIntent.addFlags( android.content.Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET ); sharingIntent.addFlags( android.content.Intent.FLAG_ACTIVITY_NEW_TASK | android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK ); sharingIntent.putExtra( android.content.Intent.EXTRA_SUBJECT, SHARE_MESSAGE ); sharingIntent.putExtra( android.content.Intent.EXTRA_TEXT, url ); const pendingIntent = android.app.PendingIntent.getBroadcast( context, 0, sharingIntent, 0 ); let shareIntent = android.content.Intent.createChooser( sharingIntent, null, pendingIntent.getIntentSender() ); app.android.context.startActivity(sharingIntent); app.android.registerBroadcastReceiver( android.content.Intent.ACTION_SEND, function () { console.log("Text has been sent"); } );

PiyushHM avatar Jan 26 '21 15:01 PiyushHM

@NathanWalker I see tag enhancement for this feature. Please let us know approximate ETA of next release with this feature. OR please suggest any workaround to work this in Android based on promise, as mentioned in your comment.

As per the library files, shareUrl function is void and not promise.

PiyushHM avatar Jan 29 '21 05:01 PiyushHM

Hey there, for anyone that still needs this, I built upon the module to support callbacks in the form of promises. https://www.npmjs.com/package/nativescript-social-share-v2

It is a backwards-compatible module that can be swapped in, in place of @nativescript/social-share

This native extension waits for a callback from the social share dialogs, and returns a boolean indicating whether the share was completed, and a sharing method, indicating where it was shared to.

iOS calls the UIActivityViewController.CompletionWithItemsHandler native method which requires iOS 8 (minimum) https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/completionwithitemshandler

Android uses a PendingIntent and a registerBroadcastReceiver event listener to listen for sharing method selections. This is supported as of Lollipop (API Level 22). So the minSdkVersion must be 22 or higher. See https://developer.android.com/training/sharing/send#share-interaction-data

I needed this to use in our app quickly, so I created it as a new module, but if anyone wants to open a PR into the community version, that would be pretty awesome. Hope it helps :)

LewisSmallwood avatar May 10 '22 07:05 LewisSmallwood