UC浏览器 分享到朋友圈和微信好友

用手机UC浏览器访问新浪微博,会注意到有这样的两个分享按钮:javascript

在手机端浏览器里,点击分享按钮,就能够启动微信客户端并分享到微信。研究了下其源代码,存在这样的一个js:http://mjs.sinaimg.cn/wap/module/share/201504071745/js/addShare.min.jscss

从里面抽离出了分享调用的方法,方便调用。(注意:这个分享功能只在UC手机浏览器有效)html

 

if (/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))) {  
        if (window.location.href.indexOf("?mobile") < 0) {  
            try {  
                //判断是手机端访问  
                if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {  
  
                //判断是UC浏览器  
                    if (typeof (ucweb) != "undefined") {  
                          
                        //微信好友  
                        $("#btnShareFirend").unbind();  
                        $("#btnShareFirend").bind("click", function () {  
                            var Browser = new Object();  
                            Browser.ios = /iphone/.test(Browser.userAgent); //判断ios系统   
                            var title = document.title;  
                            var img = "";  
                            var url = location.href;  
                            if (Browser.ios) {  
                                ucbrowser.web_share(title, img, url, 'kWeixin', '', '', '');  
                            } else {  
                                ucweb.startRequest("shell.page_share", [title, img, url, 'WechatFriends', '', '', '']);  
                            };  
                        });  
  
                        //微信朋友圈  
                        $("#btnWeixinShare").unbind();  
                        $("#btnWeixinShare").bind("click", function () {  
                            var Browser = new Object();  
                            Browser.ios = /iphone/.test(Browser.userAgent); //判断ios系统   
                            var title = document.title;  
                            var img = "";  
                            var url = location.href;  
                            if (Browser.ios) {  
                                ucbrowser.web_share(title, img, url, 'kWeixinFriend', '', '', '');  
                            } else {  
                                ucweb.startRequest("shell.page_share", [title, img, url, 'WechatTimeline', '', '', '']);  
                            };  
                        });  
                           
                    }  
                }  
            } catch (e) { }  
        }  
    }  

 

完整代码:java

<html>  
<body>  
    <script src="jquery.min.js"></script>    
    <h4>测试微信分享功能(仅在手机版UC浏览器下有效)</h4>   
    <input type="button" class="btnShareFriends" style='margin: 20px auto; width: 100%;height:50px;' value='分享给微信好友'> </input>  
    <br/>   
    <input type="button" class="btnWeixinShare" style='margin: 20px auto; width: 100%;height:50px;' value='分享到微信朋友圈'> </input>  
    <script type="TEXT/javascript">  
      $(function () {  
    if (/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))) {  
        if (window.location.href.indexOf("?mobile") < 0) {  
  
            //判断是手机端访问  
            if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {  
  
                //判断是UC浏览器  
                if (typeof (ucweb) != "undefined") {  
  
                    $(".btnShareFriends").click(function () {  
                        var Browser = new Object();  
                        Browser.ios = /iphone/.test(Browser.userAgent); //判断ios系统   
                        var title = "测试分享到朋友圈";  
                        var img = "";  
                        var url = location.href;  
                        if (Browser.ios) {  
                            ucbrowser.web_share(title, img, url, 'kWeixin', '', '@39yst', '');  
                        } else {  
                            ucweb.startRequest("shell.page_share", [title, img, url, 'WechatFriends', '', '', '']);  
                        }   
                    });  
  
                    $(".btnWeixinShare").click(function () {  
                        var Browser = new Object();  
                        Browser.ios = /iphone/.test(Browser.userAgent); //判断ios系统   
                        var title = "测试分享到朋友圈";  
                        var img = "";  
                        var url = location.href;  
                        if (Browser.ios) {  
                            ucbrowser.web_share(title, img, url, 'kWeixinFriend', '', '@39yst', '');  
                        } else {  
                            ucweb.startRequest("shell.page_share", [title, img, url, 'WechatTimeline', '', '', '']);  
                        }  
                    });  
                }  
            } else {  
                alert("请使用手机UC浏览器测试");  
            }  
        } else {  
            alert("请使用手机访问测试");  
        }  
    }  
});  
    </script>  
</body>  
</html>