I received a request last week. The request is as follows: when the user scans the QR code, a link will be generated. The link will send a request to the backend and return an apk download address. The user can click the download button to download the apk. Then a problem occurred. After testing, it was found that the apk could not be downloaded by clicking the download button on the page opened by scanning WeChat. After Baidu found out, it turned out that the built-in browser of WeChat blocked the download link. After communicating with the demander, the demand was changed to if If the user opens the page using WeChat's built-in browser, the user is prompted to change the browser to open the page, otherwise the apk cannot be downloaded. So how to determine whether the user is using WeChat browser?
We know that js can obtain browser-related information through window.navigator.userAgent, such as: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/ 537.36, then we can also use this method to obtain relevant information about WeChat’s built-in browser: Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11d201 MicroMessenger/ 5.3. Use the keyword MicroMessenger to determine whether it is WeChat’s built-in browser. The judgment function is as follows: