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

前端 未结 9 2499
礼貌的吻别
礼貌的吻别 2020-12-09 14:42

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 websi

相关标签:
9条回答
  • 2020-12-09 15:18

    you can do it by getting the UserAgent

    string ua = Request.UserAgent;
    if (ua != null && (ua.Contains("iPhone") || ua.Contains("iPad")))
    {
    ...
    ...
    ...
    }
    
    0 讨论(0)
  • 2020-12-09 15:20

    From iOS 13 the user agent changed to Mac OS, for example:

    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15

    0 讨论(0)
  • 2020-12-09 15:24

    For iPad user agent is something like:

    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 for iPhone its somthing like:

    Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3

    Any many more depending on the version and wheather its iPhone 3 or 4

    so better just do a substring search for iPhone and iPad as suggested by another answer

    0 讨论(0)
  • 2020-12-09 15:25

    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 ;)
    }
    
    0 讨论(0)
  • 2020-12-09 15:27

    The user-agent for these devices includes "iPod", "iPad" or "IPhone" as appropriate. Note that there are several user agents in play, so an exact match is unwise - but have a look from your device at http://whatsmyuseragent.com

    So check the user-agent in the headers.

    0 讨论(0)
  • 2020-12-09 15:28
    private static final Pattern IPHONE_AGENT = Pattern.compile(".*iPad.*|.*iPhone.*|.*iPod.*");    
    
    String userAgent = request.getHeader("User-Agent");
    if (userAgent != null && IPHONE_AGENT.matcher(userAgent).matches()) {
        // do something
    }
    
    0 讨论(0)
提交回复
热议问题