The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
How to get the User-Agent string in windows phone 8/ Windows 10 Mobile RRS feed

  • Question

  • Hi All,

    How to retrieve the user-agent string of Device? Which is the best option from the below:

    1. Hard code the string based on OS version:

    public static string GetDeviceUserAgent()
            {
                if (System.Environment.OSVersion.Version.Major < 8)
                {
                    //OS: Before Windows Phone 8
                    //source : http://blogs.windows.com/windows_phone/b/wpdev/archive/2011/08/29/introducing-the-ie9-on-windows-phone-mango-user-agent-string.aspx  
                    return "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)";
                }
                else if (System.Environment.OSVersion.Version.Major == 8)
                {
                    if (System.Environment.OSVersion.Version.Minor < 10)
                    {
                        //OS: Windows Phone 8
                        //Source : http://social.msdn.microsoft.com/Forums/en-US/windowsdeveloperpreviewgeneral/thread/6be392da-4d2f-41b4-8354-8dcee20c85cd/
                        return "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)" ;
                    }
                    else
                    {
                        //OS: Windows Phone 8.1
                        //Source : http://msdn.microsoft.com/en-us/library/ie/hh869301(v=vs.85).aspx
                        return  "Mozilla/5.0 (Windows Phone 8.1;Trident/7.0; Touch; rv:11; IEMobile/11.0) like Gecko" ;
                    }
                }
                else
                {
                    //OS: Windows 10
                    return "Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; NOKIA; Lumia 730 Dual SIM) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Mobile Safari/537.36 Edge/14.14332" ;
                }
            }

    Problem: The Revision will be static. So the the AppleWebKit, Chrome, Mobile Safari and Edge revision numbers always static.

    2. Retrieve "user-agent" string from browser:

     

    public static string UserAgent; public void LoadUserAgent() { string Html = @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN""> <html> <head> <script language=""javascript"" type=""text/javascript""> function notiUserAgent() { window.external.notify(navigator.userAgent); } </script> </head> <body onload=""notiUserAgent();""></body> </html>"; var tempWebBrowser = new Microsoft.Phone.Controls.WebBrowser(); tempWebBrowser.IsScriptEnabled = true; tempWebBrowser.Visibility = System.Windows.Visibility.Collapsed; tempWebBrowser.Loaded += (sender, args) => tempWebBrowser.NavigateToString(Html); tempWebBrowser.ScriptNotify += (sender, args) => { UserAgent = args.Value;

    //Giving "Mozilla/5.0 (Windows Phone 8.1; ARM; Trident/8.0; Touch; rv:11.0; WebBrowser/8.1; IEMobile/11.0; NOKIA; Lumia 730 Dual SIM) like Gecko" LayoutRoot.Children.Remove(tempWebBrowser); }; LayoutRoot.Children.Add(tempWebBrowser); }


    Problem: 

    The Windows Phone 8 app not giving Edge Browser "user-agent". It is giving old IE user-agent.

     Thanks,


    Pallam Madhukar Windows Phone Developer


    Tuesday, May 17, 2016 12:15 PM

All replies

  • Hi Pallam Madhukar,

    There is no official way to get the user agent for different OS version. Using System.Environment.OSVersion to do this should not be a good practice in WP/UWP dev.
    Could you tell us why do you need to get the user-agent?  Maybe we can find other ways to achieve your goal.

    Best Regards,
    Leon Guang



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 18, 2016 9:23 AM
  • Hi Leon Gaung,

    We have developed Library where my customers uses for application development. The user-agent is the one of the property of device info.  All other platforms like Android, iOS and Web are able to get this information.

    could you please tell us why System.Enviroment.OSVersion is not good for use? 

    Any other suggestions on building user-agent string?


    Pallam Madhukar Windows Phone Developer

    Wednesday, May 18, 2016 2:48 PM
  • Hi Leon Guang,

    My Library is developed in Windows Phone Silverlight, So I can't use the UWP API. I think the problem when we use the first approach is: It is static, It always return windows 10 user-agent for all Windows 10/x OS's, Like Windows 10.1, Windows 11, etc..

    Do you think any other problem. As we don't have other option we have to bear with hard coded string and update library code for new OS release's.

    Thanks,


    Pallam Madhukar Windows Phone Developer

    Friday, May 27, 2016 5:20 PM