Reputation: 191
Short
Does anyone know of a way to determine the physical size of an iPhone (in inches) via code?
We want to have a slightly different UI on the iPhone 6+ and any future devices that are at least as large. The complication is that these are running in non-native mode so the screen bounds are always 320x568 units for the 6+, 6 and 5's.
Long
From what I can tell there is no way to do this (and maybe this is a conscious decision by Apple)?
I can identify an iPhone 6+ via its device name (iPhone7,1) and that's fine for now, but I can only guess at how to identify future devices. For example, if there's an iPhone 7+ will that have a device name of "iPhone8,1" and will it be as large as the 6+?
Ideally, I'd just ask the device for its size (height / width) in inches and use that to decide which UI to use. Or, if I had the dpi, I could figure out inches from that.
I can't use the new size classes for this since we're doing this in a portrait UI and the 5, 6, 6+ are all "compact" in that orientation...
There are various "tricks" that might work but these seem unreliable. This includes assuming that nativeScale >= 3 or landscape size class == "regular" ==> iPhone 6+ or larger.
As things stand currently, we will probably have to default to our 5 / 6 UI for any future devices that the code doesn't recognize (until we can do an update)...
Any ideas?
Related Links:
iOS Different Font Sizes within Single Size Class for Different Devices
Upvotes: 1
Views: 110
Reputation: 2716
I'm using this code im my pch file:
#define IS_IPAD ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
#define IS_IPHONE6PLUS (!IS_IPAD && [[UIScreen mainScreen] bounds].size.height >= 736)
#define IS_IPHONE6 (!IS_IPAD && !IS_PHONE6PLUS && [[UIScreen mainScreen] bounds].size.height >= 667)
#define IS_IPHONE5 (!IS_IPAD && !IS_PHONE6PLUS && !IS_IPHONE6 && ([[UIScreen mainScreen] bounds].size.height >= 568))
then in your code you can ask
if (IS_IPHONE6PLUS) {
// do something
}
output from NSStringFromCGRect([[UIScreen mainScreen] bounds])
is {{0, 0}, {414, 736}}
Upvotes: 1
Reputation: 535232
determine the physical size of an iPhone (in inches)
This will never be possible, because the physical screen is made up of pixels (square LEDs), and there is no way to ask the size of one of those.
Thus, for example, an app that presents a ruler (inches / centimetres) would need to know in some other way how the pixel count relates to the physical dimensions of the screen - for example, by looking it up in a built-in table. As you rightly say, if a new device were introduced, the app would have no entry in that table and would be powerless to present a correctly-scaled ruler.
Upvotes: 2