> For the complete documentation index, see [llms.txt](https://developers-apps-in-toss.toss.im/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/common/growth/share/share-message.md).

# 消息分享

`分享` 通过该函数，用户可以轻松分享内容，因此可以显示原生分享表单。例如，用户可以从已安装的应用列表中选择想要的应用（如：Messenger、备忘录应用）来分享邀请消息或文本信息。它利用各平台（Android、iOS）默认提供的分享界面。

`options.message` 当将要分享的消息传递给该属性时，会显示用户可以选择的应用列表。例如，当用户想要分享文本消息或保存到备忘录应用时会很有用。

**签名**

```typescript
function share(message: { message: string }): Promise<void>;
```

**参数**

* **options** · 必需 · `对象`

  这是一个包含要分享消息的对象。

  * **options.message** · 必需 · `string`

    这是要分享的文本字符串。例如，“你好！我分享这段内容。”

**示例：实现分享功能**

下面是一个点击按钮后分享消息的简单示例。

{% tabs %}
{% tab title="js" %}

```js
import { share } from '@apps-in-toss/web-framework';

async function handleShare() {
  try {
    await share({ message: '要分享的消息' });
    console.log('分享完成');
  } catch (error) {
    console.error('分享失败：', error);
  }
}
```

{% endtab %}

{% tab title="React" %}

```tsx
import { share } from '@apps-in-toss/web-framework';

const ShareButton = () => {
  const handleShare = async () => {
    try {
      await share({ message: '要分享的消息' });
      console.log('分享完成');
    } catch (error) {
      console.error('分享失败：', error);
    }
  };

  return <button onClick={handleShare}>分享</button>;
};
```

{% endtab %}

{% tab title="React Native" %}

```tsx
import { share } from '@apps-in-toss/framework';
import { Button } from 'react-native';

function MyPage() {
  return <Button title="分享" onPress={() => share({ message: '这是要分享的消息。' })} />;
}
```

{% endtab %}
{% endtabs %}

**接收用户输入并分享消息**

也可以实现分享用户直接输入的消息。以下是创建邀请消息功能的示例。

```tsx
import { useState } from 'react';
import { TextInput, Button, View, Alert } from 'react-native';
import { share } from '@apps-in-toss/framework';

function ShareWithInput() {
  const [invitationMessage, setInvitationMessage] = useState(''); // [!code highlight]

  const handleShare = () => {
    if (!invitationMessage.trim()) {
      Alert.alert('请输入要分享的消息。');
      return;
    }
    share({ message: invitationMessage }); // [!code highlight]
  };

  return (
    <View style={{ padding: 20 }}>
      <TextInput
        style={{
          height: 40,
          borderColor: 'gray',
          borderWidth: 1,
          marginBottom: 10,
          paddingHorizontal: 8,
        }}
        placeholder="请输入邀请消息"
        value={invitationMessage}
        onChangeText={setInvitationMessage}
      />
      <Button title="分享邀请消息" onPress={handleShare} />
    </View>
  );
}
```

**体验示例应用**

[apps-in-toss-examples](https://github.com/toss/apps-in-toss-examples) 在仓库中 [with-share-text](https://github.com/toss/apps-in-toss-examples/tree/main/with-share-text) 下载代码，或扫描下方二维码亲自体验。

二维码链接: intoss\://with-share-text


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/common/growth/share/share-message.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
