首页指南参考教程

Expo 分享 iconExpo 分享

提供实现共享文件的库。

Android
iOS
Web

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

¥expo-sharing allows you to share files directly with other compatible applications.

网络上的共享限制

¥Sharing limitations on web

  • Web 版 expo-sharing 构建于 Web Share API 之上,该 API 仍具有 浏览器支持非常有限。使用 Sharing.isAvailableAsync() 调用该 API 之前,请务必检查该 API 是否可以使用。

    ¥expo-sharing for web is built on top of the Web Share API, which still has very limited browser support. Be sure to check that the API can be used before calling it by using Sharing.isAvailableAsync().

  • 网络上需要 HTTPS:Web Share API 仅当页面通过 https 提供时在 Web 上可用。使用 npx expo start --https 运行你的应用以启用它。

    ¥HTTPS required on web: The Web Share API is only available on web when the page is served over https. Run your app with npx expo start --https to enable it.

  • 没有网络上的本地文件共享:通过 URI 共享本地文件适用于 Android 和 iOS,但不适用于 Web。你无法通过 URI 在网络上共享本地文件 - 你需要将它们上传到某个地方并共享该 URI。

    ¥No local file sharing on web: Sharing local files by URI works on Android and iOS, but not on web. You cannot share local files on web by URI — you will need to upload them somewhere and share that URI.

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

¥Sharing to your app from other apps

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

¥Currently expo-sharing only supports sharing from your app to other apps and you cannot register to your app to have content shared to it through the native share dialog on native platforms. You can read more in the related feature request. You can setup this functionality manually in Xcode and Android Studio and create an Expo Config Plugin to continue using Expo Prebuild.

安装

¥Installation

Terminal
npx expo install expo-sharing

If you are installing this in an existing React Native app (bare workflow), start by installing expo in your project. Then, follow the additional instructions as mentioned by library's README under "Installation in bare React Native projects" section.

API

import * as Sharing from 'expo-sharing';

Methods

Sharing.isAvailableAsync()

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)

NameTypeDescription
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

NameTypeDescription
UTI
(optional)
string
Only for:
iOS

Uniform Type Identifier

  • the type of the target file.
dialogTitle
(optional)
string
Only for:
Android
Web

Sets share dialog title.

mimeType
(optional)
string
Only for:
Android

Sets mimeType for Intent.

Expo 中文网 - 粤ICP备13048890号