1、在微信开放平台创建应用获得wechatappid
2、安装cordova-plugin-wechat插件
ionic cordova plugin add cordova-plugin-wechat@2.1.0 --variable wechatappid=微信appid
(补充:我之前用的cordova-plugin-wechat@2.3.0 版本 打包的时候报错,使用as查看代码,插件带有错误。)
2.3.0版本错误修改
(1)getAppId方法没有参数,部分地方调用时却加了参数,去掉参数。
(2)getAppId方法是静态方法,静态方法中不能使用非静态的变量。
appId = new Wechat().preferences.getString(WXAPPID_PROPERTY_KEY, "");
(3)分享代码如下:
//微信分享
shareWx(type){
//type:0微信好友,1朋友圈
let wechat = (<any>window).Wechat;
wechat.isInstalled(
function(installed) {
if (!installed) {
this.toastService.show("您没有安装微信!");
return;
}
},
function(reason) {
this.toastService.show("Failed: " + reason);
}
);
wechat.share(
{
message: {
title: "分享的标题",
description: "简单描述!",
thumb:"图片链接",
media: {
type: wechat.Type.WEBPAGE,
webpageUrl:"分享后点击查的url"
}
},
scene: type=="0"?wechat.Scene.SESSION:wechat.Scene.TIMELINE // share to SESSION
},
function() {
console.log("分享成功");
},
function(reason) {
console.log("分享失败");
}
);
}
(4)注意:微信分享测试时必须对app进行签名,签名要和开放平台中注册的签名一致。
先压缩再签名:ionic cordova build android --prod --release
可以在android studio里面签名,也可以用别的签名工具签名。
网友评论