Search code examples
javascriptmobilehybrid-mobile-app

possible to detect webapp or mobile browser in javascript


I am working with web application (iOS/Android)

Basically, My application looks like

  • Basically it is just webpage
  • Application wrapped it with WebView
  • So technically, user can access the page with mobile browser(like Safari, Chrome, etc even PC Web browser) and Mobile Application

what I trying to do is detecting request from user is form Mobile Application or mobile browser.(with javascript)

Is it even possible? If yes, recommend the method(I prefer native javascript since project is commercial)

Thanks.

P.S. I do not need device detect with navigator.userAgent, purpose of this job is detecting mobile browser or mobile app, not which device user uses.


Solution

  • You can add a custom user agent string for your application webview so that you can distinguish a webview from js For android you can use

    myWebView.getSettings().setUserAgentString(<your custom user agent string>);
    

    It is better to append extra string to the actual useragent string

    myWebView.getSettings.getSettings().setUserAgentString(myWebView.getSettings().getUserAgentString() + <your custom user agent string>);
    

    You can check whether the user agent string contains the custom extra string from JavaScript.