William Jockusch
William Jockusch

Reputation: 27295

html/iOS -- have a link resolve to something different depending on whether device is iPhone or iPad

Yeah, I could detect it in Objective C and then modify the html accordingly. But can I put it directly into the html? So if the user clicks the link on an iPhone (or iPod Touch), they go to

http://link-for-iphone-users.com

But if they click the same link on an iPad, they instead go to

http://link-for-ipad-users.com

EDIT: To clarify, the html in question ships with the app. If the html detection is a mess, I'll just go the UIDevice route.

Upvotes: 0

Views: 125

Answers (4)

Telmo Marques
Telmo Marques

Reputation: 5106

I'm not sure what you're trying to do here, but I'm going to assume the HTML is loaded externally to the device.

If this is the case, you can get the user agent string and set the link appropriately using javascript (or server-side, as very much well said by Hivebrain).

The user agent string for an iPad might look something like this:

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

The iPhone one might look like this:

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

You can get the user agent string using navigator.userAgent. Having this, should be easy to write a simple if condition that check's the user agent and sets the link accordingly.

On the other hand, if the HTML isn't loaded externally to the device, I personally see no point on putting the condition right on the HTML.

Some references:

http://www.htmlgoodies.com/beyond/webmaster/toolbox/article.php/3888106/How-Can-I-Detect-the-iPhone--iPads-User-Agent.htm

What is the iPad user agent?

http://www.w3schools.com/jsref/prop_nav_useragent.asp

Upvotes: 1

Legolas
Legolas

Reputation: 12325

Whether you are on an iPhone or a iPod Touch:

UIDevice *device = [UIDevice currentDevice];
NSString *systemName = [device systemName];

To detect the version of the OS:

UIDevice *device = [UIDevice currentDevice];
NSString *systemVersion = [device systemVersion];

Upvotes: 0

Hivebrain
Hivebrain

Reputation: 784

It would be simplist to just do the detection on the device using the [UIDevice currentDevice].model call described in Jack Lawrence's answer.

If you really want to use one URL though, you could handle the detection on the server-side by looking at the user agent string in the HTTP headers of the request that the user makes to your one url. Requests from the iPad look something like this:

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 the "iPad" designation.

You could write your website to check the user agent for the device information and then redirect as needed.

Upvotes: 2

Jack Lawrence
Jack Lawrence

Reputation: 10772

Check out the UIDevice class docs.

NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType isEqualToString:@"iPhone"])
    // set url to an iPhone url

Upvotes: 0

Related Questions