目录

Sharing

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

{"name": "system.share"}

Module Import

Add the following configuration to the script element on the page where the API will be called:

import share from '@system.share'

Or

var share = require("@system.share")

Application Scope

Item

Description

Applicable devices

Mobile phones and tablets

Applicable regions

Global

Definition

API

Description

share.share(OBJECT)

Shares data with other apps.

[h2]share.share(OBJECT)

Description

Shares data with other apps.

OBJECT parameters

Parameter

Type

Mandatory

Description

type

string

Yes

MIME type of the data. The value must be in lowercase.

data

string

Yes

Shared data.

  • If the value of type is a MIME type starting with text/ (for example, text/plain), the value of data is the text content to be shared.
  • If type is set to other values, the value of data is the path of the file to be shared.

The following file paths are supported:

  • Path of the file downloaded through fetch.fetch
  • File path obtained through file.save or list
  • In-app resource file path starting with a slash (/)

success

function

No

Callback upon success. Most native apps do not return the sharing status correctly, so the cancel callback function instead of the success callback function may run even if the sharing is successful.

fail

function

No

Callback upon failure.

cancel

function

No

Callback upon cancellation.

complete

function

No

Callback upon completion.

Sample code

share.share({
  type:"text/html",
  data:"<b>bold</b>",
  success:function(data){console.log("handling success");},
  fail: function(data, code) {
    console.log("handling fail, code=" + code);
  }
});

Demo

<template>
  <div class="container">
      <div class="item-content">
        <text class="txt">Share text:</text>
        <input style="flex: 1; font-size: 30px;" value="{{input_key}}" placeholder="Please enter something to share" onchange="shareContent" />
      </div>
      <input type="button" class="btn" onclick="shareText"  value="Click to share text" />
      <input type="button" class="btn" onclick="sharelocal"  value="Share In-app files" />
      <input type="button" class="btn" onclick="media_pickImage"  value="Select image to share" />
  </div>
</template>

<style>
  .container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }

  .item-content {
    background-color: #ffffff;
    padding: 30px;
    margin-bottom: 100px;
    align-items: center;
  }

  .txt {
    width: 200px;
    text-align: right;
    font-weight: bold;
  }
   .btn { 
    height: 80px;
    width: 300px;
    text-align: center;
    border-radius: 5px;
    margin-right: 60px;
    margin-left: 60px;
    margin-bottom: 50px;
    color: #ffffff;
    font-size: 30px;
    background-color: #0faeff;
  }
</style>

<script>
  import share from '@system.share'
  import media from '@system.media'
  import prompt from '@system.prompt'

  export default {
    data: {
      componentName: 'share',
      shareData: '',
    },
    onInit: function () {
      this.$page.setTitleBar({ text: 'share' })
    },
    shareContent: function (e) {
      this.shareData = e.text
    },
    shareText: function () {
      share.share({
        type: 'text/html',
        data: this.shareData,
        success: function () {
          prompt.showToast({ message: 'share success' })
          console.info('share success')
        },
        fail: function (erromsg, errocode) {
          prompt.showToast({ message: 'share fail --- ' + errocode + ':' + erromsg })
          console.info('share fail --- ' + errocode + ':' + erromsg)
        },
        cancel: function () {
          prompt.showToast({ message: 'share cancel' })
          console.info('share cancel  ')
        },
        complete: function () {
          prompt.showToast({ message: 'share complete ' })
          console.info('share complete  ')
        }
      })
    },

    sharelocal: function () {
      share.share({
        type: 'image/*',
        data: '/Common/logo.png',
        success: function () {
          prompt.showToast({ message: 'share local success' })
          console.info('share local success')
        },
        fail: function (erromsg, errocode) {
          prompt.showToast({ message: 'share local fail --- ' + errocode + ':' + erromsg })
          console.info('share local fail --- ' + errocode + ':' + erromsg)
        },
        cancel: function () {
          prompt.showToast({ message: 'share local cancel' })
          console.info('share cancel  ')
        },
        complete: function () {
          prompt.showToast({ message: 'share local complete' })
          console.info('share complete  ')
        }
      })
    },

    media_pickImage: function () {
      media.pickImage({
        success: function (ret) {
          console.log('fetch--------callback ' + ret.uri)
          share.share({
            type: 'image/jpg',
            data: ret.uri,
            success: function () {
              prompt.showToast({ message: 'share pickImage success' })
              console.info('share pickImage success')
            },
            fail: function (erromsg, errocode) {
              prompt.showToast({ message: 'share pickImage fail --- ' + errocode + ':' + erromsg })
              console.info('share pickImage fail --- ' + errocode + ':' + erromsg)
            },
            cancel: function () {
              prompt.showToast({ message: 'share pickImage cancel' })
              console.info('share pickImage cancel  ')
            },
            complete: function () {
              prompt.showToast({ message: 'share pickImage complete ' })
              console.info('share pickImage complete  ')
            }
          })
        },
        cancel: function () {
        },
        fail: function () {
        },
        complete: function () {
        }
      })
    }
  }
</script>

The following figure shows the final effect.