跳至主要內容
版本:v8

@capacitor/share

分享 API 提供方法,讓您在使用者可能已安裝的任何啟用分享功能的應用程式中分享內容。

分享 API 適用於 iOS、Android 和網頁(使用新的 Web Share API),但目前網頁支援尚不完善。

安裝

npm install @capacitor/share
npx cap sync

Android

預設情況下,Capacitor 應用程式僅允許分享快取資料夾中的檔案。若要讓其他 Android 資料夾可分享,必須將它們加入 android/app/src/main/res/xml/file_paths.xml 檔案中。請查看 FileProvider 文件中的指定可用檔案區段,以了解可用的位置。

範例

import { Share } from '@capacitor/share';

await Share.share({
title: 'See cool stuff',
text: 'Really awesome thing you need to see right meow',
url: 'https://ionic.dev.org.tw/',
dialogTitle: 'Share with buddies',
});

// Share text only
await Share.share({
text: 'Really awesome thing you need to see right meow',
});

// Share url only
await Share.share({
url: 'https://ionic.dev.org.tw/',
});

// Share local file using url parameter
const photo = await Camera.getPhoto(options);
await Share.share({
url: photo.path,
});

// Share multiple files using files parameter
const { photos } = await Camera.pickImages(options);
await Share.share({
files: photos.map(photo => photo.path!),
});

每個平台使用不同的欄位組合,但您應全部提供。

API

canShare()

canShare() => Promise<CanShareResult>

檢查是否支援分享。

回傳: Promise<CanShareResult>

1.1.0


share(...)

share(options: ShareOptions) => Promise<ShareResult>

顯示分享模式視窗,以與其他應用程式分享內容

參數類型
optionsShareOptions

回傳: Promise<ShareResult>

1.0.0


介面

CanShareResult

屬性類型描述
value布林值是否支援分享。1.1.0

ShareResult

屬性類型描述
activityType字串接收分享動作的應用程式識別碼。在某些情況下可能為空字串。在網頁上將會是未定義。1.0.0

ShareOptions

屬性類型描述
title字串設定任何訊息的標題。若分享至電子郵件,這將會是主旨1.0.0
text字串設定要分享的文字1.0.0
url字串設定要分享的 URL,可以是 http、https 或 file:// URL1.0.0
files字串陣列要分享的檔案之 file:// URL 陣列。僅支援 iOS 和 Android。4.1.0
dialogTitle字串設定分享模式視窗的標題。此選項僅在 Android 上支援。1.0.0