What's new
Apple iPad Forum 🍎

Welcome to the Apple iPad Forum, your one stop source for all things iPad. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Explaining the user agent string

ardchoille

iPF Novice
Joined
Apr 21, 2012
Messages
1,471
Reaction score
446
Many users are at a loss as to how Safari will always render the mobile version of a website rather than the desktop version. I found some interesting information in the Apple Developer pages that helps explain what is happening to cause this behavior.

Using the Safari User Agent String

A browser sends a special string, called a user agent, to websites to identify itself. The web server, or JavaScript in the downloaded webpage, detects the client’s identity and can modify its behavior accordingly. In the simplest case, the user agent string includes an application name—for example, Navigator as the application name and 6.0 as the version. Safari on the desktop and Safari on iOS have their own user agent strings, too.

The Safari user agent string for iOS is similar to the user agent string for Safari on the desktop except for two additions: It contains a platform name and the mobile version number. The device name is contained in the platform name. For example, you can detect iOS and the specific device such as iPad. Typically, you do not send iPhone-specific web content to an iPad since it has a much larger screen. Note that the version numbers in this string are subject to change over time as new versions of iOS become available, so any code that checks the user agent string should not rely on version numbers.

For example, Listing 2-3 shows the user agent string for an iPhone running iOS 2.0 and later, where the string XXXX is replaced with the build number.

Listing 2-3 iPhone running on iOS 2.0 user agent string
Code:
Mozilla/5.0 (iPhone; U; CPU iOS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/XXXXX Safari/525.20

The parts of the Safari on iOS user agent string are as follows:

(iPhone; U; CPU iOS 2_0 like Mac OS X; en-us)
The platform string. iPhone is replaced with iPod when running on an iPod touch and iPad when running on an iPad.

AppleWebKit/525.18.1
The WebKit engine build number.

Version/3.1.1
The Safari family version.

Mobile/XXXXX
The mobile version number, where XXXX is the build number.

Safari/525.20
The Safari build number.

For example, the user agent string for an iPod touch contains iPod in the platform name as shown in Listing 2-4.

Listing 2-4 iPod touch running iOS 1.1.3 user agent string
Code:
Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3
The user agent string for an iPad contains iPad in the platform name as shown in Listing 2-5.

Listing 2-5 iPad running iOS 3.2 user agent string
Code:
Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10
Note that the user agent string is slightly different for earlier Safari on iOS releases. Listing 2-6 shows the user agent string for an iPhone running iOS 1.1.4 and earlier. Note that the platform string does not contain the iOS version number.

Listing 2-6 iPhone running iOS 1.0 user agent string
Code:
Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543 Safari/419.3
 

Most reactions

Top