我一直在搜索关于内置微信浏览器生成的用户代理字符串的某种文档。

我做了很多非常特定的浏览器检测,但找不到与微信传递给网站的UA字符串远程相关的任何内容。

这将遵循以下思路:

Mozilla / 5.0(iPhone; CPU OS 6_0,例如Mac OS X)AppleWebKit / 536.26(KHTML,例如Gecko)版本/6.0 Mobile / 10A5355d Safari / 8536.25

有谁知道有没有办法区分iOS上的Safari和iOS上的微信内置浏览器? (或者甚至有可能)

任何建议将不胜感激!

最佳答案

我发现我有微信的JS API(WeChat),所以我已经编辑了答案:
http://mp.weixin.qq.com/qa/index.php?qa=search&q=weixinjsbridge

长故事短,只需将其添加到您的js中:

document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
    // bridge initialized, meaning we're in WeChat, not stand-alone browser...
}, false);

还有一些API可以即时共享并与特定的 friend 共享,并在成功共享时获取回调。

附言

刚刚发现,在iOS微信上,桥接的初始化方式比在Android上更快,因此此回调永远不会被调用,因为在初始化桥接后添加了侦听器。

因此,为了完整回答,这是如何正确执行操作:
// when your webapp is loaded, before adding listener for weixing js bridge, check if it's already initialized:
var timeoutID = 0;
if( typeof WeixinJSBridge !== "undefined" )
{
    // WeChat JS bridge already initialized. Wonderful.
}
else
{
    // setup a time out of let's say 5 seconds, to wait for the bridge:
    timeoutID = window.setTimeout(WeChatBridgeTimeout,5000);
    // now add listener for the bridge:
    document.addEventListener('WeixinJSBridgeReady', WeChatBridgeReady, false);
}

// Now in bridge time out:
function WeChatBridgeTimeout()
{
     // Just to be sure that bridge was not initialized just before the line we added the listener (since it's a separate process than JS), let's check for it again:
      if( typeof WeixinJSBridge !== "undefined" )
      {
           // WeChat JS bridge already initialized. Wonderful.
      }
      else
      {
           // Nope... if it's not initialized by now, we're not in WeChat.
      }
}

// And in event handled:
function WeChatBridgeReady()
{
     // remove listener timeout
     window.clearTimeout(timeoutID);
     // WeChat JS bridge initialized.
}

10-04 19:42