35

Sorry, I would just test this myself, but I'm currently without my mac. Does a web request made inside of a UIWebView send the same user-agent info that a web request made from mobile Safari would?

dl.
  • 2,264
  • 1
  • 19
  • 22

3 Answers3

42

Web requests made from UIWebView will not include the word "Safari" in the User Agent string. Web requests made from Mobile Safari will. This is the best way I have found for determining of a request is coming from within an app or from Mobile Safari.

Sample User Agent from UIWebView within App:

User-Agent: Mozilla/5.0 (iPad; U; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile

Sample User Agent from Mobile Safari:

User-Agent: Mozilla/5.0 (iPad; U; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari
Jens Kohl
  • 5,899
  • 11
  • 48
  • 77
dl.
  • 2,264
  • 1
  • 19
  • 22
  • 3
    Android's webview sends the http header "X-Requested-With". The stand alone browser does not. Does iPhone's UIWebView do this as well? see http://stackoverflow.com/a/15254092/90236 – Michael Levy Apr 29 '13 at 17:53
  • 1
    Unfortunately this answer fails with Chrome iOs. These are the UAs for Safari and Chrome. Both contain "Safari" ——— `Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D201 Safari/9537.53` ——— `Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_1 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) CriOS/35.0.1916.38 Mobile/11D201 Safari/9537.53` – fregante Jun 12 '14 at 22:26
  • 1
    @bfred.it I don't understand your comment. The question had nothing to do with distinguishing different mobile browsers from each other. Only from distinguishing an embedded UIWebView from the built-in browser. – Johann Oct 08 '14 at 16:15
  • 3
    @Johann Chrome/iOS is not a different web browser, it's a UIWebView wrapper. Since Chrome's UIWebView contains "Safari" like Safari itself does, you can't tell *it* (a UIWebView) apart from Safari by checking for that word (like the answer suggests to do) – fregante Oct 10 '14 at 10:06
  • 5
    does anyone know if this answer is still valid in the year 2020? – Mobigital Feb 05 '20 at 21:34
15

Standalone mobile Safari user agent strings contain the word 'Version', whereas uiWebView user agent strings do not. So, the detection script can be modified to work with the latest version of iOS like so:

var is_uiwebview = /(iPhone|iPod|iPad).*AppleWebKit(?!.*Version)/i.test(navigator.userAgent);
unceus
  • 237
  • 4
  • 6
0

This is the generic one. I have used it for iPhone and iPad. it will use the device's current model and current system version.

let genericUserAgent = “Mozilla/5.0 (iPhone; CPU iPhone OS like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Mobile/15E148 Safari/604.1”

    var userAgent = genericUserAgent
    let deviceModel = UIDevice.current.model
    let systemVersion = UIDevice.current.systemVersion

    if deviceModel.contains(“iPad”) {
        userAgent = “Mozilla/5.0 (iPad; CPU OS \(systemVersion) like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/\(systemVersion) Mobile/15E148 Safari/604.1”
    } else {
        userAgent = “Mozilla/5.0 (iPhone; CPU iPhone OS \(systemVersion) like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/\(systemVersion) Mobile/15E148 Safari/604.1"
    }

    self.wkWebview.customUserAgent = userAgent
vivek s
  • 91
  • 1
  • 3