uni.shareWithSystem
调用系统分享组件发送分享消息,不需要配置分享SDK。
平台差异说明
| App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序 | 飞书小程序 | QQ小程序 | 京东小程序 | 元服务 | 小红书小程序 |
|---|---|---|---|---|---|---|---|---|---|---|
| √(App 2.6.4+) | x | x | x | x | x | x | x | x | x | x |
shareWithSystem 兼容性
- HarmonyOS: 支持 (HBuilderX 4.31+)
方法定义
uni.shareWithSystem(options)
参数说明
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| type | String | 否 | 分享类型,只支持 text、image,默认为 text |
| summary | String | 否 | 分享的文字内容 |
| href | String | 否 | 分享链接,iOS端分享到微信时必填此字段 |
| imageUrl | String | 否 | 分享图片,仅支持本地路径 |
| success | Function | 否 | 接口调用成功的回调 |
| fail | Function | 否 | 接口调用失败的回调函数 |
| complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
注意事项
-
Android端图片分享:
- 当
msg参数中设置图片(imageUrl属性)时,分享类型自动变为image - 在分享时可能只会发送图片(如微信)
- 没有设置图片时分享类型则认为是文本
text - Android端高版本无法分享私有路径的图片,只能分享来自相册的图片(使用
uni.chooseImage选择图像时请设置为原图) - 新版本Android的文件权限有较大的调整,如遇到无法分享的情况,推荐使用 uts插件 来解决
- 当
-
iOS端分享:
- iOS端不同的分享程序对分享内容有要求
- 如微信分享时必需添加链接地址
href,否则微信分享失败 - iOS 8.0及以上系统触发成功回调则表示发送消息成功
-
分享回调说明:
success回调表示分享操作完成,但不一定代表用户实际分享成功- 用户可能选择了取消或选择了其他操作
- 建议在
fail回调中提供降级方案(如复制到剪切板)
代码示例
基础用法 - 分享文本和链接
// 分享文本内容及链接
uni.shareWithSystem({
summary: '这是要分享的文字内容',
href: 'https://uniapp.dcloud.io',
success() {
console.log('分享完成')
uni.showToast({
title: '分享成功',
icon: 'success'
})
},
fail(err) {
console.error('分享失败', err)
uni.showToast({
title: '分享失败',
icon: 'none'
})
}
})
实际应用场景 - 访客二维码分享
// 分享访客信息及二维码链接
const shareToVisitor = () => {
// 构建分享内容
const shareContent = '尊敬的来宾您好,您的访客信息已生成:\n' +
'访客姓名:name\n' +
'接待业主:owner\n' +
'业主地址:1幢1单元1\n' +
'拜访时间:2025.11.04 15:28 至 2025.12.09 15:28\n' +
'定位导航:https://ditu.amap.com/...\n' +
'访客二维码:'
// 构建分享链接
const shareUrl = 'https://example.com'
// 调用系统分享
uni.shareWithSystem({
type: 'text',
summary: shareContent,
href: shareUrl, // iOS端分享到微信时必填
success: (res) => {
console.log('分享成功', res)
},
fail: (error) => {
console.error('分享失败:', error)
// 降级方案:复制到剪切板
uni.setClipboardData({
data: shareContent + shareUrl,
success: () => {
uni.showToast({
title: '分享内容已复制到剪切板',
icon: 'none',
duration: 3000
})
}
})
},
complete: () => {
console.log('分享操作完成')
}
})
}
分享图片
// 选择图片并分享
uni.chooseImage({
count: 1,
sizeType: ['original'], // 使用原图
success: (res) => {
const tempFilePath = res.tempFilePaths[0]
uni.shareWithSystem({
type: 'image',
imageUrl: tempFilePath, // 仅支持本地路径
success() {
console.log('图片分享成功')
},
fail(err) {
console.error('图片分享失败', err)
}
})
}
})
条件编译使用
// #ifdef APP-PLUS
uni.shareWithSystem({
summary: '分享内容',
href: 'https://example.com',
success() {
console.log('分享成功')
},
fail() {
// 降级处理
uni.setClipboardData({
data: '分享内容',
success: () => {
uni.showToast({
title: '内容已复制到剪切板',
icon: 'none'
})
}
})
}
})
// #endif
// #ifndef APP-PLUS
// 非App环境使用复制到剪切板
uni.setClipboardData({
data: '分享内容',
success: () => {
uni.showToast({
title: '内容已复制到剪切板',
icon: 'none'
})
}
})
// #endif
常见问题
Q1: 为什么分享到微信时链接没有显示?
A: iOS端分享到微信时,必须设置 href 参数,否则分享会失败或链接不显示。
Q2: Android端图片分享失败怎么办?
A:
- 确保图片路径是本地路径(使用
uni.chooseImage获取) - 使用原图大小,不要压缩
- 如果仍然失败,考虑使用 uts 插件
Q3: 分享成功回调是否表示用户真的分享了?
A: 不一定。success 回调只表示分享操作流程完成,用户可能选择了取消。建议在 fail 回调中提供降级方案。
Q4: 如何实现分享到指定平台(如只分享到微信)?
A: uni.shareWithSystem 调用的是系统分享面板,用户可以选择分享平台。如果需要指定平台,需要使用 plus.share 或配置相应的分享SDK。
相关API
- uni.setClipboardData - 设置剪贴板内容(降级方案)
- uni.chooseImage - 选择图片(用于图片分享)
- plus.share - 原生分享插件(需要配置SDK)
运行效果和遇到的问题
- 成功调用系统分享:
- 分享成功后点击返回三方工具不生效:
没有报错,就是返回不了三方应用。有没有大佬知道怎么处理