Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Detection for mobile device
Message
 
À
28/12/2015 16:15:17
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01629341
Message ID:
01629349
Vues:
43
I would advise against trying to do server side device detection. Build your UI so that it can adapt to device size or use media queries on the client to decide how style content differently.

There are a million combinations of user agents and if you skip new user agents you are bound to piss off people in the future.

And if you really do want to do this there are libraries natively in .NET as well as for JavaScript that let you get reliable and updated browser detection. If you use anyhting use that so the burden of keeping your user agents updated isn't on your code.

These days the only browser detection I ever do is to check for old version of IE (pre 10). Everything else is detectable with other means more easily and usually it's not the device you care about by the size of the screen (device-width).

+++ Rick ---

>You probably have seen the popular code for detecting a mobile device from a .NET app. I have seen it is widely used by many developers. However, I haven't seen so far one who mentioned to make sure we have a user agent. Pretty much everyone assumes we always have one. If you use the HttpWebRequest object to send a Url somewhere. On that server, the user agent object would not be initialized.
>
>So, the code is enhanbed like this:
>
>
>    Public oRequest As System.Web.HttpRequest
>
>    ' This assumes oRequest is initialized before coming here
>
>		Dim laMobileType(66) As String
>
>		cUserAgent = oRequest.UserAgent
>
>		' If this is recognized at the object level
>		If oBrowser.IsMobileDevice Then
>			lMobileBrowser = True
>		Else
>
>			' If this is recognized at the WAP level
>			If Not oRequest.ServerVariables("HTTP_X_WAP_PROFILE") Is Nothing Then
>				lMobileBrowser = True
>			Else
>
>				' If the server variable exists
>				If Not oRequest.ServerVariables("HTTP_ACCEPT") Is Nothing Then
>
>					' If this is recognized at the WAP level at this verification
>					If oRequest.ServerVariables("HTTP_ACCEPT").ToLower.Contains("wap") Then
>						lMobileBrowser = True
>					End If
>
>				End If
>
>				' If we have not recognized it yet
>				If Not lMobileBrowser Then
>
>					' If this is recognized at the user agent level
>					If Not oRequest.ServerVariables("HTTP_X_WAP_PROFILE") Is Nothing Then
>						lMobileBrowser = True
>					Else
>
>						' If we have a user agent object
>						If Not cUserAgent Is Nothing Then
>
>							' Initialization
>							laMobileType(1) = "midp"
>							laMobileType(2) = "j2me"
>							laMobileType(3) = "avant"
>							laMobileType(4) = "docomo"
>							laMobileType(5) = "novarra"
>							laMobileType(6) = "palmos"
>							laMobileType(7) = "palmsource"
>							laMobileType(8) = "240x320"
>							laMobileType(9) = "opwv"
>							laMobileType(10) = "chtml"
>							laMobileType(11) = "pda"
>							laMobileType(12) = "windows ce"
>							laMobileType(13) = "mmp/"
>							laMobileType(14) = "blackberry"
>							laMobileType(15) = "mib/"
>							laMobileType(16) = "symbian"
>							laMobileType(17) = "wireless"
>							laMobileType(18) = "nokia"
>							laMobileType(19) = "hand"
>							laMobileType(20) = "mobi"
>							laMobileType(21) = "phone"
>							laMobileType(22) = "cdm"
>							laMobileType(23) = "up.b"
>							laMobileType(24) = "audio"
>							laMobileType(25) = "SIE-"
>							laMobileType(26) = "SEC-"
>							laMobileType(27) = "samsung"
>							laMobileType(28) = "HTC"
>							laMobileType(29) = "mot-"
>							laMobileType(30) = "mitsu"
>							laMobileType(31) = "sagem"
>							laMobileType(32) = "sony"
>							laMobileType(33) = "alcatel"
>							laMobileType(34) = "lg"
>							laMobileType(35) = "eric"
>							laMobileType(36) = "vx"
>							laMobileType(37) = "NEC"
>							laMobileType(38) = "philips"
>							laMobileType(39) = "mmm"
>							laMobileType(40) = "xx"
>							laMobileType(41) = "panasonic"
>							laMobileType(42) = "sharp"
>							laMobileType(43) = "wap"
>							laMobileType(44) = "sch"
>							laMobileType(45) = "rover"
>							laMobileType(46) = "pocket"
>							laMobileType(47) = "benq"
>							laMobileType(48) = "java"
>							laMobileType(49) = "pt"
>							laMobileType(50) = "pg"
>							laMobileType(51) = "vox"
>							laMobileType(52) = "amoi"
>							laMobileType(53) = "bird"
>							laMobileType(54) = "compal"
>							laMobileType(55) = "kg"
>							laMobileType(56) = "voda"
>							laMobileType(57) = "sany"
>							laMobileType(58) = "kdd"
>							laMobileType(59) = "dbt"
>							laMobileType(60) = "sendo"
>							laMobileType(61) = "sgh"
>							laMobileType(62) = "gradi"
>							laMobileType(63) = "jb"
>							laMobileType(64) = "dddi"
>							laMobileType(65) = "moto"
>							laMobileType(66) = "iphone"
>
>							' For each mobile type
>							For lnCounter = 1 To 66
>
>								' If this is one of them
>								If cUserAgent.ToLower.Contains(laMobileType(lnCounter).ToLower) Then
>									lMobileBrowser = True
>								End If
>
>							Next
>
>						End If
>
>					End If
>
>				End If
>
>			End If
>
>		End If
>
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform