54

Have you had any experience in Facebook in-app browser detection? What's the core difference in user agent?

I don't want to know if it is a only mobile/ios/chrome. I need to know whether user agent is specific from Facebook in-app browser

milosz0010
  • 679
  • 1
  • 6
  • 7

3 Answers3

87

You can check for FBAN / FBAV in the user agent.

Check this link: Facebook user agent

Machavity
  • 30,841
  • 27
  • 92
  • 100
Jan Swart
  • 6,761
  • 10
  • 36
  • 45
  • What does ua.indexOf("FBAN") > -1 and ua.indexOf("FBAV") > -1 mean? Force out user to use web browser instead of in-apps browser? – Robbi Nespu Dec 08 '16 at 20:18
  • 1
    @RobbNesp `var us = navigator.userAgent` gives you a string value. `.indexOf()` checks for the position of a sub-string in a string. If it is not found a value of -1 is returned. It is simply checking if it is the Facebook browser, by checking if the browser's user agent contains either of the given sub strings. – Jan Swart Dec 11 '16 at 07:27
  • 8
    Thanks! Sharing snippet for Instagram also: `(ua.indexOf('Instagram') > -1)` – jeff_drumgod Oct 02 '18 at 23:43
  • I added the snippet to the answer, thanks @jeff_drumgod – svelandiag Mar 02 '20 at 17:29
  • 2
    Can you explain the `|| window.opera` bit? I don't think it would cast to the user agent string. – DrLightman Oct 15 '20 at 07:27
  • Thanks a zillion. – Jagruti May 20 '21 at 15:12
  • Does it work for both Facebook apps on Android and iOS devices? – Chilly Code Nov 28 '21 at 11:41
41

To complete worker11811's answer on using the user agent, here's a code snippet to make it work:

function isFacebookApp() {
    var ua = navigator.userAgent || navigator.vendor || window.opera;
    return (ua.indexOf("FBAN") > -1) || (ua.indexOf("FBAV") > -1);
}
Sascha Klatt
  • 1,199
  • 1
  • 13
  • 23
2

You can use https://www.npmjs.com/package/detect-inapp and check if inapp.isInApp() is true

DonnaTelloo
  • 190
  • 1
  • 11