转载地址:http://jingwentian.com/t-298
在微信上,通过公众平台推送给用户的文章,是在微信内部直接打开的,用的无外乎就是一个UIWebView控件(IOS上,Android上也差不多)。但特殊的是,微信官方在这里面加了一个默认的Js API--WeixinJSBridge,通过它,能直接在该页面上做这些操作:
1、将文章分享到微信朋友圈
2、将文章直接发送给微信上的好友
3、将文章分享到腾讯微博
4、关注指定用户
http://www.baidufe.com/item/cf5d14486fac3644e209.html
http://www.baidufe.com/item/c4bacb3bd66930f2e43e.html
http://www.baidufe.com/item/f07a3be0b23b4c9606bb.html
http://www.scscms.com/html/article/20140429-21176670.html
DEMO:
<script type="text/javascript">
var dataForWeixin={
appId: "",
img: "http://jingwentian.com/logo.png",
url: "http://jingwentian.com",
title: "文哥的博客",
desc:"这是文哥的博客",
fakeid: "",
};
(function(){
var onBridgeReady=function(){
// 发送给好友;
WeixinJSBridge.on('menu:share:appmessage', function(argv){
WeixinJSBridge.invoke('sendAppMessage',{
"appid": dataForWeixin.appId,
"img_url": dataForWeixin.img,
"img_width": "120",
"img_height": "120",
"link": dataForWeixin.url,
"desc": dataForWeixin.desc,
"title": dataForWeixin.title
}, function(res){});
});
// 分享到朋友圈;
WeixinJSBridge.on('menu:share:timeline', function(argv){
WeixinJSBridge.invoke('shareTimeline',{
"img_url":dataForWeixin.img,
"img_width":"120",
"img_height":"120",
"link":dataForWeixin.url,
"desc":dataForWeixin.desc,
"title":dataForWeixin.title
}, function(res){});
});
// 分享到微博;
WeixinJSBridge.on('menu:share:weibo', function(argv){
WeixinJSBridge.invoke('shareWeibo',{
"content":dataForWeixin.title+' '+dataForWeixin.url,
"url":dataForWeixin.url
}, function(res){});
});
// 分享到Facebook
WeixinJSBridge.on('menu:share:facebook', function(argv){
WeixinJSBridge.invoke('shareFB',{
"img_url":dataForWeixin.img,
"img_width":"120",
"img_height":"120",
"link":dataForWeixin.url,
"desc":dataForWeixin.desc,
"title":dataForWeixin.title
}, function(res){});
});
};
if(document.addEventListener){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if(document.attachEvent){
document.attachEvent('WeixinJSBridgeReady' , onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady' , onBridgeReady);
}
})();
</script>