Detecting different hardware at runtime is useful for analytics (among other, even more questionable purposes).
Many iOS app creators may be interested to know how many users are experiencing their app on an iPad mini (rather than just knowing how many users are experiencing their app on an iPad with 1024x768 screen resolution - which would also be interesting).
Is there any public API in Cocoa touch/UIKit/ObjC/C
which could be used to detect that your iOS app is running on an iPad mini at runtime? Ideally, this method should distinguish between iPad 2 & iPad mini (which have the same number of pixels, but a different pixel density).
Post Script: I realize many people will consider detecting iPad mini at runtime a bad idea. However, I think this is a valid question with a definite Yes or No answer. An answer which I think it is useful for the community to know.
import CoreTelephony.framework. Try this on device
This solves my purpose up to now. But as I don't have any iPad mini I don't know what it gonna print for iPad mini. Could you let me know?
Use sysctlbyname to get the platform string, then compare it with the IPSW prefix strings listed here. It looks like the only currently known iPad Mini is "iPad2,5"
Example:
Boxel's answer would be good if it didn't invoke undefined behavior and if it didn't have superfluous parts. One,
+ [NSString stringWithCString:encoding:]
requires a C string - that is, a NUL-terminated char pointer (else it will most likely dump core). Also, you don't need the conversion toNSString
- sincesysctlbyname()
provides you with a plain old C string (without the NUL terminator, of course), you can directly usestrcmp()
to save a few dozen CPU cycles:Edit: now that answer is fixed as well.
There is no reliable, future-proof way to detect the iPad mini's pixel density. Other answers suggest looking at the
hw.machine
string. But we don't know (as of the time I write this) what the strings will be for the iPad mini cellular models (though we can make an educated guess:iPad2,6
will probably have GSM andiPad2,7
will probably have CDMA).It's fine to look at the
hw.machine
string for analytics. But it's dangerous to let it affect your app's user interface, because even theiPad2,5
string for the current iPad mini is subject to change.When the iPad 2 came out, the wifi model's string was
iPad2,1
. Later (when they released the iPad 3), they changed the iPad 2 hardware and changed thehw.machine
string toiPad2,4
, but they still called it the iPad 2. The same thing could happen with the iPad mini - or even with the iPad 2 again! For example, Apple could release yet another version of the iPad 2 hardware and give it the machine stringiPad2,8
.