Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if the useragent is an ipad or iphone?

I'm using a C# asp.net website.

How can I check if the user using ipad or iphone? How can I check the platform?

For example, if the user enter the website from ipad I'd like to display"Hello ipad user"

like image 654
avnic Avatar asked Mar 01 '11 13:03

avnic


1 Answers

UPDATE on 17-07-2020: it looks like Apple removed the word iPad and now use Macintosh instead

UPDATE: Since the iPad user agent contains the word iPhone as @Rob Hruska mentioned:

Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10

and iPhone user agent is something like this:

Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7

it would be correct to check for the word iPhone; or iPad; to identify the device:

var userAgent = HttpContext.Current.Request.UserAgent.ToLower(); if (userAgent.Contains("iphone;")) {     // iPhone } else if (userAgent.Contains("ipad;") || userAgent.Contains("macintosh;")) {     // iPad } else {     // Think Different ;) } 
like image 156
Oleks Avatar answered Oct 11 '22 06:10

Oleks