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

前言

微信作为腾讯一张航空母舰一样的船票实在是时间黑洞,现在如果你开发一个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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 人的生活状态有很多,有一种人的生活状态,我称之为“活死人”。这种人如茫茫人海中的蝼蚁,生活的并不高明,可是...
    CC_f1d9阅读 562评论 0 3
  • 日子总有苦涩与酸楚 需要一茶匙白糖 例如番茄炒蛋 要加糖 加点葱花? 不许加! 就一点点? 不行! 没葱花不好吃啊...
    Jason同学阅读 327评论 0 2
  • 雅思听力核心词汇21.minibus英 ['mɪnɪbʌs] 美 ['mɪnɪbʌs]• n. 面包车(...
    造物家英语阅读 422评论 0 1