Reference version

This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo 分享 iconExpo 分享

一个用于实现文件共享的库。

Android
iOS
Web
Included in Expo Go
Recommended version:
~14.0.8

expo-sharing 允许你直接与其他兼容的应用共享文件。

Web 上的共享限制

  • expo-sharing 的 web 版本基于 Web Share API,但该 API 目前在浏览器中的支持仍然 非常有限。在调用之前,请务必使用 Sharing.isAvailableAsync() 检查该 API 是否可用。
  • Web 端需要 HTTPS:Web Share API 仅在网页通过 https 提供服务时才可用。请使用 npx expo start --tunnel 运行你的应用以启用它。
  • Web 端不支持本地文件共享:通过 URI 共享本地文件在 Android 和 iOS 上可行,但在 web 上不可行。你不能在 web 上通过 URI 共享本地文件——你需要先将它们上传到某处,然后共享该 URI。

从其他应用共享到你的应用

目前 expo-sharing 仅支持从你的应用共享到其他应用,你无法在原生平台上注册你的应用,使其通过原生共享对话框接收共享内容。你可以在 相关功能请求 中了解更多。你可以在 Xcode 和 Android Studio 中手动设置此功能,并创建一个 Expo 配置插件,以继续使用 Expo Prebuild

安装

Terminal
npx expo install expo-sharing

If you are installing this in an existing React Native app, make sure to install expo in your project.

API

import * as Sharing from 'expo-sharing';

Methods

Sharing.isAvailableAsync()

Android
iOS
Web

Determine if the sharing API can be used in this app.

Returns:
Promise<boolean>

A promise that fulfills with true if the sharing API can be used, and false otherwise.

Sharing.shareAsync(url, options)

Android
iOS
Web
ParameterTypeDescription
urlstring

Local file URL to share.

options(optional)SharingOptions

A map of share options.

Default:{}

Opens action sheet to share file to different applications which can handle this type of file.

Returns:
Promise<void>

Types

SharingOptions

Android
iOS
Web
PropertyTypeDescription
anchor(optional){ height: number, width: number, x: number, y: number }
Only for:
iOS

set the anchor point for iPad

dialogTitle(optional)string
Only for:
Android
Web

Sets share dialog title.

mimeType(optional)string
Only for:
Android

Sets mimeType for Intent.

UTI(optional)string
Only for:
iOS

Uniform Type Identifier

  • the type of the target file.