@ capgo/ 电容器- 份额- 目标
让用户通过iOS和Android上的系统共享表,直接从其他应用程序共享文本,链接,图像和文件到您的 Capacitor app.
🤖 建筑与AI编码代理? 安装 Capgo 技能 所以克劳德代码,代码,光标,副驾驶,或双子座CLI知道如何安装和使用
@capgo/capacitor-share-target在您的电容应用程序中:npx skills add Cap-go/capgo-skills。然后问你的经纪人: 添加@capgo/capor-share-target到我的Capacitor应用上并把它接上 代理设置指南 • 妇女 主机磁共振
主要特点
- 共享活动页:1
shareReceived听者发送标题,文本和文件. - 文件:每个共享文件都带有其URI,名称和MIME类型. 在 iOS 上, 您的共享扩展必须将这些附件细节保存到 App Group 中 .
- 机器人:通过为您想要的MIME类型设置的意向过滤器接收内容.
- iOS系统: 通过 App 组来读取您共享扩展所保存的内容
appGroupId。 。 。 . - 平台: iOS和Android. 互联网档案馆的存檔,存档日期2013-12-21. iOS需要共享扩展目标. 在网上不可用.
兼容性
| 插件版本 | 电容兼容性 | 维护 |
|---|---|---|
| 联合国 | 联合国 | ✅ |
| 页:1 | 页:1 | 应要求 |
| 页:1 | 页:1 | ❌ |
| 页:1 | 页:1 | ❌ |
说明: 此插件的主要版本沿用了Capacitor的主要版本. 使用匹配您的 Capacitor 安装的版本( 如 Capacitor 8 的插件 v8) 。 只有最新的主要版本得到积极维护.
安装
您可以使用我们的 AI 辅助设置安装插件 。 使用以下命令将 Capgo 技能添加到您的 AI 工具 :
npx skills add https://github.com/cap-go/capacitor-skills --skill capacitor-plugins
然后使用下列提示:
Use the `capacitor-plugins` skill from `cap-go/capacitor-skills` to install the `@capgo/capacitor-share-target` plugin in my project.
如果您更喜欢手动设置,请通过运行以下命令来安装插件,并遵循下面的平台特定指令:
npm install @capgo/capacitor-share-target
npx cap sync
配置
机器人
在您的 AndroidManifest.xml 内部 <activity> 允许您的应用程序接收共享内容的标签 :
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
您可以自定义 mimeType 以匹配要接收的内容类型(例如, text/*, (中文(简体) ), image/*, (中文(简体) ), video/*, (中文(简体) ), */*) (中文(简体) ).
iOS系统
对于iOS,您需要创建一个共享扩展来接收共享内容. 这需要额外设置:
- 在 Xcode 中,转到文件 > New > 目标
- 选择“ 共享扩展” 并单击下一个
- 命名( 如“ 共享扩展 ”)并单击 Finish
- 配置共享扩展以将数据保存到共享 App Group (例如,
group.com.yourcompany.app(中文(简体) ) - 配置您的插件
capacitor.config.ts或者说capacitor.config.json数字 :
{
plugins: {
CapacitorShareTarget: {
appGroupId: "group.com.yourcompany.app"
}
}
}
- 在您的 ShareView Captain.swift 中,使用密钥保存共享数据
"share-target-data"或者说"SharedData"以您的应用程序组ID 创建用户默认值
实例 ShareView Captain.swift :
import UIKit
import UniformTypeIdentifiers
class ShareViewController: UIViewController {
let APP_GROUP_ID = "group.com.yourcompany.app" // Same as in capacitor.config
let APP_URL_SCHEME = "yourapp" // Your app's URL scheme
private var texts: [[String: Any]] = []
private var files: [[String: Any]] = []
override public func viewDidLoad() {
super.viewDidLoad()
guard let extensionItem = extensionContext?.inputItems.first as? NSExtensionItem,
let attachments = extensionItem.attachments else {
self.exit()
return
}
Task {
// Process attachments...
// (See full implementation in issue examples)
// Save to UserDefaults
let shareData: [String: Any] = [
"title": extensionItem.attributedTitle?.string ?? "",
"texts": texts,
"files": files
]
let userDefaults = UserDefaults(suiteName: APP_GROUP_ID)
userDefaults?.set(shareData, forKey: "share-target-data")
userDefaults?.synchronize()
// Open main app with your URL scheme
let url = URL(string: "\(APP_URL_SCHEME)://share")!
self.openURL(url)
}
}
// ... rest of implementation
}
详细指示见 iOS 共享扩展文档。 。 。 .
使用量
import { CapacitorShareTarget } from '@capgo/capacitor-share-target';
// Listen for shared content
CapacitorShareTarget.addListener('shareReceived', (event) => {
console.log('Title:', event.title);
console.log('Texts:', event.texts);
event.files?.forEach(file => {
console.log(`File: ${file.name}`);
console.log(`Type: ${file.mimeType}`);
console.log(`URI: ${file.uri}`);
});
});
API 密码
电容共享目标插件接口.
此插件允许您的应用程序接收其他应用程序共享的内容 . 用户可以从其他应用程序中共享您的应用程序的文本, URL, 和文件 .
添加倾听器( “ 共享收到 ” ,...)
addListener(eventName: 'shareReceived', listenerFunc: (event: ShareReceivedEvent) => void) => Promise<PluginListenerHandle>
听股受来事.
注册一个在与应用程序共享内容时会调用的收听器 从另一个应用程序。 召回接收包含标题、文本和文件的事件数据.
| 参数 | 类型 | 说明 |
|---|---|---|
eventName | 'shareReceived' | 事件名称. 一定是"共享接收". |
listenerFunc | (event: ShareReceivedEvent) => void | 当内容被共享到应用时引用回调函数 . |
回返情况: Promise<PluginListenerHandle>
自: 0.1.0 (中文(简体) )
删除所有收听器( )
removeAllListeners() => Promise<void>
删除此插件的所有听众 .
自: 0.1.0 (中文(简体) )
获取 PluginVersion ()
getPluginVersion() => Promise<{ version: string; }>
获取本地 Capacitor 插件版本 .
返回当前版本的本地插件执行 .
回返情况: Promise<{ version: string; }>
自: 0.1.0 (中文(简体) )
接口
插件听筒
| Prop | 类型 |
|---|---|
remove | () => Promise<void> |
共享接收事件
与应用程序共享内容时收到的事件数据.
| Prop | 类型 | 说明 | 自兹 |
|---|---|---|---|
title | string | 共享内容的标题. | 0.1.0 (中文(简体) ) |
texts | string[] | 与应用程序共享的文本内容组合 . | 0.1.0 (中文(简体) ) |
files | SharedFile[] | 与应用程序共享的文件序列 . | 0.2.0 (中文(简体) ) |
共享文件
代表应用程序共享的文件 .
| Prop | 类型 | 说明 | 自兹 |
|---|---|---|---|
uri | string | 共享文件的 URI 。 在Android/iOS上,这将是一个文件路径或数据URL. 在网络上, 这将是一个缓存的 URL 通过获取访问 . | 0.1.0 (中文(简体) ) |
name | string | 共享文件的名称,有或没有扩展名. | 0.1.0 (中文(简体) ) |
mimeType | string | 共享文件的 MIME 类型 . | 0.1.0 (中文(简体) ) |
示例
在Android上,成功副本在应用程序缓存目录下直播 shared_files。插件删除超过24小时的复制件,并封顶保存多少个。 何时 files[].uri 是该目录下的一个路径, 在您完成处理后删除它 。 如果复制失败, files[].uri 可能是原始内容 URI .
以下是处理您的应用程序中共享内容的完整例子:
import { CapacitorShareTarget } from '@capgo/capacitor-share-target';
import { Capacitor } from '@capacitor/core';
export class ShareService {
async initialize() {
// Only add listener on native platforms
if (Capacitor.isNativePlatform()) {
await CapacitorShareTarget.addListener('shareReceived', this.handleSharedContent);
}
}
private handleSharedContent(event: ShareReceivedEvent) {
console.log('Received shared content!');
// Handle shared text
if (event.texts.length > 0) {
console.log('Shared text:', event.texts[0]);
// Process the shared text (e.g., URL, note, etc.)
}
// Handle shared files
if (event.files.length > 0) {
event.files.forEach(async (file) => {
console.log(`Processing file: ${file.name}`);
if (file.mimeType.startsWith('image/')) {
// Handle image files
await this.processImage(file.uri);
} else if (file.mimeType.startsWith('video/')) {
// Handle video files
await this.processVideo(file.uri);
}
});
}
}
private async processImage(uri: string) {
// Your image processing logic here
}
private async processVideo(uri: string) {
// Your video processing logic here
}
async cleanup() {
await CapacitorShareTarget.removeAllListeners();
}
}
捐款
见 捐款额。 。 。 .
许可证
麻省理工学院