# Mobile site detection should not rely on Safari in user agent

**URL:** https://meta.discourse.org/t/mobile-site-detection-should-not-rely-on-safari-in-user-agent/103341
**Category:** Support
**Created:** [3 december 2018 om 11:01 UTC](https://meta.discourse.org/t/mobile-site-detection-should-not-rely-on-safari-in-user-agent/103341 "2018-12-03T11:01:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mekentosj](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mekentosj/32/125889_2.png) [@mekentosj](https://meta.discourse.org/u/mekentosj)
#### Post date: [3 december 2018 om 11:01 UTC](https://meta.discourse.org/t/mobile-site-detection-should-not-rely-on-safari-in-user-agent/103341/1 "2018-12-03T11:01:55Z")

</div>

Hi all,

Thanks for the fantastic piece of software that Discourse is, it’s incredible!

We are using Discourse for the community of our app [Agenda](https://agenda.com), our users love it. This is in part because we built it straight in the app using a WKWebView on mac and iOS. Unfortunately this gives a problem on iPhones as apparently discourse fails to detect it’s running on mobile.

Compare the following screenshots taken from Mobile Safari (left) and Agenda (right) on the same device:

 ![45](https://global.discourse-cdn.com/meta/original/3X/e/6/e6f2a9f9a7b8c9c86ae917f06b6a11a9830e2d37.jpeg)

The issue seems to be caused by Discourse parsing the User Agent string (left mobile safari, right Agenda using a stock WKWebView) for the word Safari or the word Mobile:

If I indeed tell the WKWebView to set the custom user agent to the same string as mobile Safari it works correctly. Unfortunately this is not a really great solution as it would force us to spoof an incorrect user agent of an incorrect safari version etc. I hope this can be fixed (for example by also checking for the word iPhone)…

Thanks and keep up the great work!  
Alex

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [3 december 2018 om 11:11 UTC](https://meta.discourse.org/t/mobile-site-detection-should-not-rely-on-safari-in-user-agent/103341/2 "2018-12-03T11:11:07Z")

</div>

Unlikely, we have run into many complex issues with string based user agent detection in the past. If you want to 100% be seen as a mobile browser you need the word “mobile” in there from our perspective.

---

<div class="post-metadata">

### Author: ![mekentosj](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mekentosj/32/125889_2.png) [@mekentosj](https://meta.discourse.org/u/mekentosj)
#### Post date: [3 december 2018 om 11:15 UTC](https://meta.discourse.org/t/mobile-site-detection-should-not-rely-on-safari-in-user-agent/103341/3 "2018-12-03T11:15:35Z")

</div>

Nasty. FWIW if anyone hits the same issue, here’s the workaround I added, which I guess has to stay then:

```
    // we need to force a user agent that is recognized by Discourse as mobile
    webView.evaluateJavaScript("navigator.userAgent") { [weak self] result, error in
        if let userAgent = result as? String, error == nil {
            self?.webView.customUserAgent = userAgent + " Mobile"
        }
    }

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [6 oktober 2021 om 19:03 UTC](https://meta.discourse.org/t/mobile-site-detection-should-not-rely-on-safari-in-user-agent/103341/4 "2021-10-06T19:03:31Z")

</div>



---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [5 november 2021 om 19:03 UTC](https://meta.discourse.org/t/mobile-site-detection-should-not-rely-on-safari-in-user-agent/103341/5 "2021-11-05T19:03:35Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
