美文网首页Ionic2开发
Ionic2实战-微信分享功能开发

Ionic2实战-微信分享功能开发

作者: 逃离火星 | 来源:发表于2018-03-28 22:46 被阅读130次

    前言

    微信作为腾讯一张航空母舰一样的船票实在是时间黑洞,现在如果你开发一个APP没有分享到微信功能,那估计很快就会被大家抛弃。

    今天就来讲一下如何给Ionic2 APP添加分享到微信的功能。

    步骤

    1、首先需要去微信开放平台注册一个账号(https://open.weixin.qq.com/),获取一个appid的东西

    2、执行命令安装Cordova插件

    cordova plugin add cordova-plugin-wechat --variable wechatappid=YOUR_WECHAT_APPID(你的微信开放平台id)

    3、执行Cordova命令

    cordova build ios or cordova build android

    4、分享朋友圈

    shareWxSession(){
        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: this.shareImg,
            description: this.shareDesc,
            thumb: this.shareImg,
            media: {
                type: wechat.Type.LINK,
                webpageUrl: this.shareUrl
            }
        },
            scene: wechat.Scene.SESSION   // share to SESSION
        }, function () {
           this.toastService.show('分享成功');
        }, function (reason) {
            console.log("Failed: " + reason);
        });
      }
    

    5、分享微信好友

    shareWxTimeLine(){
        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: this.shareImg,
            description: this.shareDesc,
            thumb: this.shareImg,
            media: {
                type: wechat.Type.LINK,
                webpageUrl: this.shareUrl
            }
        },
            scene: wechat.Scene.TIMELINE   // share to Timeline
        }, function () {
           this.toastService.show('分享成功','bottom',4000);
        }, function (reason) {
            console.log("Failed: " + reason);
        });
    
      }
    

    最后

    通过使用强大Cordova插件,我们可以很容易的在自己APP种整合进微信分享的功能。

    插件地址:https://github.com/xu-li/cordova-plugin-wechat

    相关文章

      网友评论

      • 神豪VS勇士赢:老哥 是不是微信支付和支付宝 支付 不好做 ?
        逃离火星:@神豪VS勇士赢 这个肯定少不了
        神豪VS勇士赢:@逃离火星 是啊 我公众号什么的都申请了 发现要是做支付的话 还要认证公众号 每年交300
        逃离火星:@神豪VS勇士赢 都有现成的插件,我没做过,应该不难,不过涉及到支付的话可能流程容错性就要加强了

      本文标题:Ionic2实战-微信分享功能开发

      本文链接:https://www.haomeiwen.com/subject/amjecftx.html