Current location: Brighton, UK

Detecting what device you're on in CocoaTouch

Posted on Tuesday, February 16, 2010 @ 13:27 CET

Occasionally I have the need to know what device my app is running on (iPhone/iPod Touch) in order to expose or hide certain functionality. One app I wrote for a client recently was rejected at first because there was a "Call" button that obviously wouldn't work on an iPod Touch. Oops!

So I wrote this generic utility class to check the device you're running on with methods that not only return the platform ID, but also a quick check if you're running on an iPhone, iPod Touch or the iPhone Simulator.

The methods it contains are:

  • platformID
  • platformName
  • deviceIsiPhone
  • deviceIsiPodTouch
  • deviceIsSimulator

Dead simple, but very useful, at least for me. If it sounds like something you could use, you can grab the class here :)

Also included is a utility class I've used for a few things such as:

  • stringContainsSubstring - checks if a string contains a substring
  • showAlertWithTitle - pops up a standard UIAlert. Just one line instead of the standard 3
  • openURLInSafari - I never remember this so its a simple shortcut
  • colorForHex - very useful for converting hex color codes to UIColor

- paulo

Post a comment:

You must have Flash and JavaScript enabled to post a comment.