14

I want to open my site only in iPhone and iPod with Safari browser. For any other browser like Chrome, Dolphin, etc, It shouldn't open.

But currently I am getting the same user agent from almost all the devices:

For Safari:

User Agent String :: mozilla/5.0 (iphone; cpu iphone os 7_0_2 like mac os x) applewebkit/537.51.1 (khtml, like gecko) version/7.0 mobile/11a501 safari/9537.53

For Chrome:

User Agent String :: mozilla/5.0 (iphone; cpu iphone os 7_0_2 like mac os x) applewebkit/537.51.1 (khtml, like gecko) crios/30.0.1599.16 mobile/11a501 safari/8536.25

For Mercury browser:

User Agent String :: mozilla/5.0 (iphone; cpu iphone os 6_0_1 like mac os x) applewebkit/536.26 (khtml, like gecko) mercury/7.4.2 mobile/10a523 safari/8536.25

For Dolphin browser:

User Agent String :: mozilla/5.0 (iphone; cpu iphone os 7_0_2 like mac os x) applewebkit/537.51.1 (khtml, like gecko) version/6.0 mobile/10a523 safari/8536.25

With Chrome I can see by checking for crios, and Mercury by mercury string, but what about the other browsers which seem to have same user agent as Safari?

informatik01
  • 16,038
  • 10
  • 74
  • 104
Novice User
  • 3,552
  • 6
  • 31
  • 56

3 Answers3

29

The other answer isn't strictly correct. On iOS, Apple block any competing browsers from actually including their own rendering engine; all must use the standard webview.

So all of those 3rd party browsers (with functionality such as syncing favourites, etc.) are simply wrappers around the Safari powered webview that's actually rendering the page, hence them all having Safari's user agent string.

Community
  • 1
  • 1
Andy Nugent
  • 859
  • 7
  • 21
  • 1
    This isnt exactly true. If we do a string compare between the useragent used by Safari on iphone and the uiwebview/wkwebview on iphone, they dont match. Refer to the first answer for this SO question http://stackoverflow.com/questions/2143763/does-uiwebview-send-the-same-user-agent-in-the-request-headers-as-mobile-safari – shrutim Jan 12 '16 at 19:22
  • The final part of the user agent will change, as above based on the specific browser (Safari, UIWebView, Dolphin, etc.) but the main part that indicates the browser platform will be the same. (See https://en.wikipedia.org/wiki/User_agent#Format_for_human-operated_web_browsers) – Andy Nugent Jan 12 '16 at 19:38
  • I agree. But there are many websites that are misbehaving if the Safari portion of the user agent string is not present. Some websites that display fine in Safari iOS , misbehave on UIWebView or WKWEbview. Eg: http://m.gasbuddy.com . I had to update the WKWebview's useragent string for the webpage to render exactly same as the Safari on IOS. All I am saying is that we should be careful when we say the useragents are same for Safari and all the mobile webviews used on iOS, given that there are websites that behave differently – shrutim Jan 12 '16 at 20:08
5

The browsers do this very deliberately to prevent people from doing what you're trying to do. Their developers feel that since they're using the same web engine (WebKit, blink or gecko) as the others that they'll lkely render pages optimized for similar browsers just fine (which is likely true).

So most likely there is no way to tell.

ksimons
  • 3,797
  • 17
  • 17
0

Solves for mercury, opera, chrome n firefox but not dolphin

(
  (/(iPad|iPhone|iPod)/gi).test(userAgent) &&
  !(/CriOS/).test(userAgent) &&
  !(/FxiOS/).test(userAgent) &&
  !(/OPiOS/).test(userAgent) &&
  !(/mercury/).test(userAgent)
)
aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242