Expo 分享 iconExpo 分享

提供实现共享文件的库。

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

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

网络上的共享限制

🌐 Sharing limitations on web

  • expo-sharing 网页版是基于 Web Share API 构建的,而该 API 仍然只有非常有限的浏览器支持。在调用它之前,请务必使用 Sharing.isAvailableAsync() 检查该 API 是否可以使用。
  • 网页需要 HTTPS:只有在通过 HTTPS 提供页面时,Web Share API 才能在网页上使用。使用 npx expo start --tunnel 运行你的应用以启用它。
  • 网页上无法共享本地文件:通过 URI 共享本地文件在 Android 和 iOS 上可行,但在网页上不可行。你不能在网页上通过 URI 共享本地文件——你需要先将文件上传到某个地方,然后共享该 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, 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.