是否可以在iOS上针对微信内置浏览器的用户代理字符串?

问题描述 投票:0回答:4

我一直在四处寻找有关内置微信浏览器生成的用户代理字符串的某种文档。

我做了很多非常具体的浏览器检测,但找不到任何与微信传递到网站的 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上微信的内置浏览器? (或者如果可能的话)

如有任何建议,我们将不胜感激!

javascript ios user-agent browser-detection wechat
4个回答
5
投票

自从我发现微信(WeChat)有JS API后,我编辑了我的答案: 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可以分享朋友圈,分享给特定好友,分享成功后回调。

附注

刚刚发现在iOS微信上,bridge的初始化速度比Android上快得多,然后这个回调永远不会被调用,因为监听器是在bridge初始化后添加的。

为了完成答案,以下是正确的做法:

// 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.
}

5
投票

非常简单。只需检查用户代理即可:

if(req.headers['user-agent'].indexOf('MicroMessenger') !== -1){
  //we are in wechat browser
  your code here
}

有时微信浏览器会恶意屏蔽App Store链接。这时您需要将用户重定向到其他地方,或者引导他们在其他更友好的浏览器中打开您的页面。 :)


1
投票

目前(WeChat6.xx),ios上微信内置浏览器的UA字符串为:

... MicroMessenger/6.1.4 ...
,狩猎时:
... Safari/600.1.4

因此以下代码适用于 android 和 ios:

var isWeChat = /micromessenger/i.test(navigator.userAgent);


0
投票

对于微信用户代理...

Mozilla/5.0 (Linux; Android 10; LIO-AN00 Build/HUAWEILIO-AN00; wv) MicroMessenger 微信 QQ AppleWebKit/537.36 (KHTML, 类似 Gecko) Version/4.0 Chrome/78.0.3904.62 XWEB/2692 MMWEBSDK/200901 Mobile Safari/ 537.36

我们检查

MicroMessenger
Weixin
(不区分大小写)...

var isWeChat = /(micromessenger|weixin)/i.test(navigator.userAgent);
© www.soinside.com 2019 - 2024. All rights reserved.