admin管理员组文章数量:1565828
使用uniapp开发app,因为前期没有申请分享所需要的appid,所以经过一番百度,这里使用的是plus的系统分享。uni.share()分享api请移步官网文档。
特点:无需申请appid 但是写起来麻烦,调起应用可能会有点慢
说明:
1、ios直接走系统分享,安卓做分享的区分。这里着重说明的是安卓分享。**
2、因为微信分享的限制,分享到朋友圈只能打开朋友圈但无法把分享内容直接赋值上去,需要手动粘贴内容或者上传图片等。分享到微信只能选择其中一个类别,如分享了图片,那么文字可以复制到剪切板,但是无法直接分享给好友;如果只分享文字,则可以直接分享。**
逻辑:
- 选择分享的应用
- 检测应用是否已安装
- 下载保存图片、视频,文字复制到剪切板
- 开始分享
部分代码
定义ComponentName(主要)
// 分享到第三方
share:function(type) {
this.cancel();
this.isShare = true;
this.shareType = type;
// #ifdef APP-PLUS
plus.nativeUI.showWaiting("应用检测中...");
let ComponentName;
if(plus.os.name == "Android") {
ComponentName = plus.android.importClass('android.content.ComponentName');
}
// 分享到微信
if(type === 'wx') {
if(plus.os.name == "Android") {
this.componentname = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
}
this.isInstall('com.tencent.mm','weixin://', type, '微信');
} else if(type === 'line') {
if(plus.os.name == "Android") {// 分享到朋友圈
this.componentname = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
}
this.isInstall('com.tencent.mm','weixin://', type, '微信');
} else if(type === 'weibo') {
// 分享到微博
if(plus.os.name == "Android") {
this.componentname = new ComponentName("com.sina.weibo", "com.sina.weiboposerinde.ComposerDispatchActivity");
}
this.isInstall('com.sina.weibo','sinaweibo://', type, '微博');
} else if(type === 'QQ') {
if(plus.os.name == "Android") {
this.componentname = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
}
this.isInstall('com.tencent.mobileqq','mqq://', type, 'QQ');
} else if(type === 'space') {
if(plus.os.name == "Android") {
this.componentname = new ComponentName("com.qzone", "com.qzonex.module.operation.ui.QZonePublishMoodActivity");
}
this.isInstall('com.qzone','mqq://', type, 'QQ空间');
}
// #endif
// #ifndef APP-PLUS
this.isInstall('com.qzone','mqq://', 'space', 'QQ空间');
// #endif
},
检测应用安装
// 验证app是否安装
isInstall:function(packageName, action, type, msg) {
// this.save(true, type);
if(plus.runtime.isApplicationExist({pname:packageName,action:action})){
plus.nativeUI.closeWaiting();
this.save(true, type);
}else{
plus.nativeUI.closeWaiting();
this.$api.msg('您还未安装'+msg+', 请先进行安装')
}
}
保存素材
uni.downloadFile({
url: item,
success:function(res) {
uni.getImageInfo({
src: res.tempFilePath,
success:function(image){
_this.filePath.push(image.path)
}
})
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath
})
}
})
分享(主要)
uniShareHandle:function(type) {
let _this = this;
// 设备信息
let platform = uni.getSystemInfoSync().platform;
let localComponentName;
if(platform === 'ios') {
// ios直接调用系统分享
// #ifdef APP-PLUS
plus.share.sendWithSystem({
type: 'image',
content: _this.copyWord,
pictures: _this.filePath
},function() {
_this.$api.msg('分享成功');
}, function(e) {
_this.$api.msg('分享失败');
})
// #endif
} else {
if(type != 'line') {
let Intent = plus.android.importClass('android.content.Intent');
let ComponentName = plus.android.importClass('android.content.ComponentName');
let ArrayList = plus.android.importClass('java.util.ArrayList');
let Uri = plus.android.importClass('android.Uri');
let Environment = plus.android.importClass('android.os.Environment');
let File = plus.android.importClass('java.io.File');
let MediaStore = plus.android.importClass("android.provider.MediaStore");
let act = plus.android.runtimeMainActivity();
let intent = new Intent();
intent.setComponent(this.componentname);
intent.setFlags("android.intent.FLAG_ACTIVITY_NEW_TASK");
let localArrayList = new ArrayList();
for(let i =0;i< this.filePath.length;i++){
let filePath = plus.io.convertLocalFileSystemURL(this.filePath[i].replace('file://', ''));
let img = Uri.parse(MediaStore.Images.Media.insertImage(act.getContentResolver(), new File(filePath).getAbsolutePath(), null, null))
localArrayList.add(img);
}
if(this.imageList.length != 0) {
intent.setAction("android.intent.action.SEND_MULTIPLE");
intent.setType("image/*");
intent.putParcelableArrayListExtra("android.intent.extra.STREAM", localArrayList);
} else {
intent.setAction("android.intent.action.SEND");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, _this.copyWord);
}
act.startActivity(intent);
} else {
// 直接打开微信,手动进行朋友圈分享
plus.runtime.launchApplication({
pname: 'com.tencent.mm',
action: 'weixin://'
})
}
}
},
版权声明:本文标题:uniapp 分享到微信、QQ、朋友圈 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1725532521a1028074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论