Implementing Feature

As Sharing feature supports multiple sharing sources (Email, Facebook, SMS, WhatsApp, Twitter), different composers are available to populate data.

Fill data with a Composer

Different Composers are available to fill the data for each service. Composers available are

  • MessageShareComposer - Compose data for sharing via Messaging Service
  • MailShareComposer - Compose data for sharing via Email Service
  • FacebookShareComposer - Compose data for sharing via Facebook
  • WhatsAppShareComposer - Compose data for sharing via WhatsApp
  • TwitterShareComposer - Compose data for sharing via Twitter
  • SocialShareSheet - Compose data for sharing via Facebook & Twitter
  • ShareSheet - Compose data for sharing via all services
Present the composer

Once a composer data is populated, present it with ShowView method to start sharing.

NPBinding.Sharing.ShowView(COMPOSER_INSTANCE, FINISHED_SHARING_CALLBACK);
COMPOSER_INSTANCE

Create an instance of any composer mentioned above and pass to ShowView

FINISHED_SHARING_CALLBACK

All sharing actions optionally take callback method which will be called once sharing window is closed. A common implementation of callback is as below.

void FinishedSharing (eShareResult _result)
{
    Debug.Log("Finished sharing");
    Debug.Log("Share Result = " + _result);
}

Share via Messaging (SMS)

For sharing with SMS, you first check if the service is available on target platform.

 NPBinding.Sharing.IsMessagingServiceAvailable()

Above returns true if service is available.

For sharing, create MessageShareComposer and fill in the details.

// Create composer
MessageShareComposer    _composer    = new MessageShareComposer();
_composer.Body                        = SMS_BODY_MESSAGE;
_composer.ToRecipients                = ARRAY_OF_RECIPIENTS;

Open Share window by below code :

NPBinding.Sharing.ShowView(_composer, FinishedSharing);

Above call triggers FinishedSharing Callback once sharing is done.

Callback Implementation
void FinishedSharing (eShareResult _result)
{
    Debug.Log("Finished sharing");
    Debug.Log("Share Result = " + _result);
}

Share via Email

For sharing with Email, you first check if the service is available on target platform.

 NPBinding.Sharing.IsMailServiceAvailable()

Above returns true if service is available.

For sharing, create MailShareComposer to fill in the details and pass it to ShowView. MailShareComposer provides option to fill in :

  • To Receipients List
  • CC Receipients List
  • BCC Receipients List
  • Mail Body
  • HTML Body (set IsHTMLBody to true if you pass html content in Body parameter)
  • Attach any type of attachment (attachment type supported or not depends on the sharing service)

Once sharing is finished, FinishedSharing Callback will be called.

MailShareComposer    _composer    = new MailShareComposer();
_composer.Subject                = m_mailSubject;
_composer.Body                    = m_plainMailBody;

// Set below to true if the body is HTML
_composer.IsHTMLBody            = false;

// Send array of receivers if required
_composer.ToRecipients            = m_mailToRecipients;
_composer.CCRecipients            = m_mailCCRecipients;
_composer.BCCRecipients            = m_mailBCCRecipients;

// Add below line if you want to attach screenshot. Else, ignore.
// _composer.AttachScreenShot();

// Add below line if you want to attach a file, for ex : image. Else, ignore.
// _composer.AddAttachmentAtPath(GET_FULL_IMAGE_PATH, MIMEType.kPNG);

// Use below line if you want to add any other attachment format. Else, ignore.
// _composer.AddAttachment(ATTACHMENT_DATA_IN_BYTE_ARRAY, ATTACHMENT_FILE_NAME, MIME_TYPE);

// Show share view
NPBinding.Sharing.ShowView(_composer, FinishedSharing);

MIME_TYPE for some file types are

PNG : image/png or use MIMEType.PNG

PDF : application/pdf or use MIMEType.PDF

ZIP : application/zip


Share on Social Network (Facebook & Twitter)

Social Share provides facility to share below types on Facebook & Twitter:

  • Text Message
  • URL
  • Image

Warning

Some services won't allow sharing some of the attributes or combined properties above as per their policies. For ex: Facebook won't allow prefilled text in the share dialog. !!!!

// Create share sheet
SocialShareSheet _shareSheet     = new SocialShareSheet();    
_shareSheet.Text                = m_shareMessage;

// Add below line if you want to share URL
_shareSheet.URL                    = m_shareURL;

// Add below line if you want to share a screenshot
_shareSheet.AttachScreenShot();

// Add below line if you want to share an image from a specified path.
_shareSheet.AttachImageAtPath(IMAGE_PATH);

// Show composer
NPBinding.UI.SetPopoverPointAtLastTouchPosition(); // To show popover at last touch point on iOS. On Android, its ignored.
NPBinding.Sharing.ShowView(_shareSheet, FinishedSharing);

For sharing, create SocialShareSheet to fill in the details and pass it to ShowView. SocialShareSheet provides option to fill in :

  • Text message to share
  • URL to share
  • Screenshot
  • Texture
  • Image at path

Once sharing is finished, FinishedSharing Callback will be called.

// Create share sheet
SocialShareSheet _shareSheet     = new SocialShareSheet();    
_shareSheet.Text                = m_shareMessage;

// Add below line if you want to share URL
_shareSheet.URL                    = m_shareURL;

// Add below line if you want to share a screenshot
_shareSheet.AttachScreenShot();

// Add below line if you want to share an image from a specified path.
_shareSheet.AttachImageAtPath(IMAGE_PATH);

// Show composer
NPBinding.UI.SetPopoverPointAtLastTouchPosition(); // To show popover at last touch point on iOS. On Android, its ignored.
NPBinding.Sharing.ShowView(_shareSheet, FinishedSharing);

Note

Some services won't allow more than one data to share. If you are trying to share message and url, ONLY one of those can be shared successfully. This is a limitation of that service and not from the plugin.


Share on Facebook exclusively

This feature allows to share on Facebook alone.

Check availability of the service with :

NPBinding.Sharing.IsFBShareServiceAvailable();

Above returns true if Facebook app is installed and ready to share.

Share Text Message [Non-possible on Facebook]

It's not allowed to share pre-filled text as per Facebook Policy.

Share URL
 private void ShareURLOnFB ()
  {
      // Create share sheet
      FBShareComposer _composer    = new FBShareComposer();
      _composer.URL                = m_shareURL;

      // Show composer
      NPBinding.Sharing.ShowView(_composer, FinishedSharing);
  }
Share Screenshot
private void ShareScreenshotOnFB ()
{
    // Create composer
    FBShareComposer _composer    = new FBShareComposer();
    _composer.AttachScreenShot();

    // Show share view
    NPBinding.Sharing.ShowView(_composer, FinishedSharing);
}
Share Image
private void ShareImageOnFB ()
{
    // Create composer
    FBShareComposer _composer    = new FBShareComposer();
    _composer.Text                = m_shareMessage;
    _composer.AttachImageAtPath(GetImageFullPath());

    // Show share view
    NPBinding.Sharing.ShowView(_composer, FinishedSharing);
}

Share on Twitter exclusively

This feature allows to share on Twitter alone.

Check availability of the service with :

NPBinding.Sharing.IsTwitterShareServiceAvailable();

Above returns true if Twitter app is installed and ready to share.

Share Text Message
private void ShareTextMessageOnTwitter ()
{
    // Create composer
    TwitterShareComposer _composer    = new TwitterShareComposer();
    _composer.Text                    = m_shareMessage;

    // Show share view
    NPBinding.Sharing.ShowView(_composer, FinishedSharing);            
}
Share URL
private void ShareURLOnTwitter ()
{
    // Create share sheet
    TwitterShareComposer _composer    = new TwitterShareComposer();
    _composer.Text                    = m_shareMessage;
    _composer.URL                    = m_shareURL;

    // Show composer
    NPBinding.Sharing.ShowView(_composer, FinishedSharing);            
}
Share Screenshot
private void ShareScreenshotOnTwitter ()
{
    // Create composer
    TwitterShareComposer _composer    = new TwitterShareComposer();
    _composer.Text                    = m_shareMessage;
    _composer.AttachScreenShot();

    // Show share view
    NPBinding.Sharing.ShowView(_composer, FinishedSharing);            
}
Share Image
private void ShareImageOnTwitter ()
{
    // Create composer
    TwitterShareComposer _composer    = new TwitterShareComposer();
    _composer.Text                    = m_shareMessage;
    _composer.AttachImageAtPath(GetImageFullPath());

    // Show share view
    NPBinding.Sharing.ShowView(_composer, FinishedSharing);            
}

Share on WhatsApp

This feature allows to share on WhatsApp alone.

Check availability of the service with :

NPBinding.Sharing.IsWhatsAppServiceAvailable();

Above returns true if WhatsApp app is installed and ready to share.

Share Text Message
private void ShareTextMessageOnWhatsApp ()
{
    // Create composer
    WhatsAppShareComposer _composer    = new WhatsAppShareComposer();
    _composer.Text                    = m_shareMessage;

    // Show share view
    NPBinding.Sharing.ShowView(_composer, FinishedSharing);            
}
Share Screenshot
private void ShareScreenshotOnWhatsApp ()
{
    // Create composer
    WhatsAppShareComposer _composer    = new WhatsAppShareComposer();
    _composer.AttachScreenShot();

    // Show share view
    NPBinding.Sharing.ShowView(_composer, FinishedSharing);    
}
Share Image
private void ShareImageOnWhatsApp ()
{
    // Create composer
    WhatsAppShareComposer _composer    = new WhatsAppShareComposer();
    _composer.AttachImageAtPath(GetImageFullPath());

    // Show share view
    NPBinding.Sharing.ShowView(_composer, FinishedSharing);    
}

Share via Share Sheet (More generic sharing)

This allows sharing using all services. Optionally you can set ExcludedShareOptions attribute to exclude some services of your choice.

Share Text Message
private void ShareTextMessageUsingShareSheet ()
{
    // Create share sheet
    ShareSheet _shareSheet     = new ShareSheet();    
    _shareSheet.Text        = m_shareMessage;

    // Set this list if you want to exclude any service/application type. Else, ignore.
    _shareSheet.ExcludedShareOptions    = m_excludedOptions;

    // Show composer
    NPBinding.UI.SetPopoverPointAtLastTouchPosition();
    NPBinding.Sharing.ShowView(_shareSheet, FinishedSharing);
}
Share URL
private void ShareURLUsingShareSheet ()
{
    // Create share sheet
    ShareSheet _shareSheet     = new ShareSheet();    
    _shareSheet.Text        = m_shareMessage;
    _shareSheet.URL            = m_shareURL;

    // Set this list if you want to exclude any service/application type. Else, ignore.
    _shareSheet.ExcludedShareOptions    = m_excludedOptions;

    // Show composer at last touch point
    NPBinding.UI.SetPopoverPointAtLastTouchPosition();
    NPBinding.Sharing.ShowView(_shareSheet, FinishedSharing);
}
Share ScreenShot
private void ShareScreenShotUsingShareSheet ()
{
    // Create share sheet
    ShareSheet _shareSheet     = new ShareSheet();    
    _shareSheet.Text        = m_shareMessage;

    // Set this list if you want to exclude any service/application type. Else, ignore.
    _shareSheet.ExcludedShareOptions    = m_excludedOptions;

    // Attaching screenshot here
    _shareSheet.AttachScreenShot();

    // Show composer
    NPBinding.UI.SetPopoverPointAtLastTouchPosition();
    NPBinding.Sharing.ShowView(_shareSheet, FinishedSharing);
}
Share Image
private void ShareImageAtPathUsingShareSheet ()
{
    // Create share sheet
    ShareSheet _shareSheet     = new ShareSheet();    
    _shareSheet.Text        = m_shareMessage;
    _shareSheet.AttachImageAtPath(GetImageFullPath());

    // Show composer
    NPBinding.UI.SetPopoverPointAtLastTouchPosition();
    NPBinding.Sharing.ShowView(_shareSheet, FinishedSharing);
}

results matching ""

    No results matching ""