Class ShareApi

java.lang.Object
com.oorian.html.js.JavaScriptApi
com.oorian.html.js.share.ShareApi

public class ShareApi extends JavaScriptApi
Provides access to the Web Share API from server-side Java code.

The Web Share API allows sharing content (text, URLs, files) to native sharing targets such as other apps on the device. This API is primarily useful on mobile devices where native sharing dialogs are common.

Browser Support:

  • Chrome for Android (full support)
  • Safari on iOS (full support)
  • Edge on Windows (limited support)
  • Firefox (limited support)

Usage:


 public class MyPage extends HtmlPage implements ShareListener
 {
     @Override
     protected void createBody(Body body)
     {
         Button shareBtn = new Button("Share");
         shareBtn.registerListener(this, MouseClickedEvent.class);
         body.addElement(shareBtn);
     }

     @Override
     public void onEvent(MouseClickedEvent event)
     {
         ShareApi.share(this, "Check this out!", "My App", "https://example.com");
     }

     @Override
     public void onEvent(ShareSuccessEvent event)
     {
         showMessage("Content shared successfully!");
     }

     @Override
     public void onEvent(ShareErrorEvent event)
     {
         showMessage("Share failed: " + event.getMessage());
     }
 }
 
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • ShareApi

      public ShareApi()
  • Method Details

    • share

      public static void share(ShareListener listener, String text, String title, String url)
      Shares content using the native share dialog with current page context.
      Parameters:
      listener - The listener that will receive share events.
      text - The text content to share (optional).
      title - The title of the shared content (optional).
      url - The URL to share (optional).
    • share

      public static void share(HtmlPage page, ShareListener listener, String text, String title, String url)
      Shares content using the native share dialog.
      Parameters:
      page - The page context.
      listener - The listener that will receive share events.
      text - The text content to share (optional).
      title - The title of the shared content (optional).
      url - The URL to share (optional).
    • shareText

      public static void shareText(ShareListener listener, String text)
      Shares text content using the native share dialog with current page context.
      Parameters:
      listener - The listener that will receive share events.
      text - The text content to share.
    • shareText

      public static void shareText(HtmlPage page, ShareListener listener, String text)
      Shares text content using the native share dialog.
      Parameters:
      page - The page context.
      listener - The listener that will receive share events.
      text - The text content to share.
    • shareUrl

      public static void shareUrl(ShareListener listener, String url)
      Shares a URL using the native share dialog with current page context.
      Parameters:
      listener - The listener that will receive share events.
      url - The URL to share.
    • shareUrl

      public static void shareUrl(HtmlPage page, ShareListener listener, String url)
      Shares a URL using the native share dialog.
      Parameters:
      page - The page context.
      listener - The listener that will receive share events.
      url - The URL to share.
    • shareUrl

      public static void shareUrl(ShareListener listener, String title, String url)
      Shares a URL with title using the native share dialog with current page context.
      Parameters:
      listener - The listener that will receive share events.
      title - The title of the shared content.
      url - The URL to share.
    • shareUrl

      public static void shareUrl(HtmlPage page, ShareListener listener, String title, String url)
      Shares a URL with title using the native share dialog.
      Parameters:
      page - The page context.
      listener - The listener that will receive share events.
      title - The title of the shared content.
      url - The URL to share.
    • checkSupport

      public static void checkSupport()
      Checks if the Web Share API is supported by the browser with current page context.

      Note: This executes JavaScript that logs to console. For programmatic checks, use feature detection on the client side.

    • checkSupport

      public static void checkSupport(HtmlPage page)
      Checks if the Web Share API is supported by the browser.

      Note: This executes JavaScript that logs to console. For programmatic checks, use feature detection on the client side.

      Parameters:
      page - The page context.