1

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

Community
  • 1
  • 1
ETA
  • 181
  • 1
  • 6
  • It is semi possible to determine DPI by calling [[UIScreen mainScreen] scale] but it returns an integer... – jesses.co.tt May 01 '15 at 15:40
  • the long and short of it is that as an Apple developer, you have ample notice to look into scale/density/form factor issues with beta versions of the SDK ... – jesses.co.tt May 01 '15 at 15:41
  • So for current devices, scale is going to return 2 for iPhone 5 and iPhone 6 (and also iPhone 4S). It will also return 2 for the iPhone 6+ in our case since we aren't running in native mode (the 6 and 6+ are basically scaled-up iPhone 5's). I believe nativeScale returns 3 for an iPhone 6+ (even in non-native mode and even in the simulator) but assuming that a future device that does @3x is as large as an iPhone 6+ seems unreliable... :( – ETA May 01 '15 at 16:02
  • 1
    We're also in a somewhat unique situation where we have 400+ (!) apps released and counting. So having to re-release all of these to support a new device is problematic. One approach we are considering is to basically move the device information onto a server so we could update that as new devices come out without having to re-release apps... – ETA May 01 '15 at 16:05

2 Answers2

2

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • This seems doubtful but I was hoping that there was some API call that would return this information for us. So, on a 6, I'd get back 2.3 inches for the width. Or, if I could get back ppi=326 and horizontal pixels=750 then I could do the math (750p/326ppi = 2.3 inches). – ETA May 01 '15 at 15:52
  • I understand what you were hoping and I'm replying that there is no such API. If you have a compelling use case, do file an enhancement request with Apple! – matt May 01 '15 at 15:53
  • I was mainly reacting to your "This will never be possible..." comment. This is totally possible (and doesn't involve needing to know the size of a pixel) - *if* the API were to expose the necessary information. – ETA May 01 '15 at 15:58
  • Expose? This assumes that the API _has_ the necessary information. But software doesn't necessarily know anything about external hardware. – matt May 01 '15 at 16:07
  • I think we are in agreement. For this to work, Apple would have to make it possible for the software to get this information from the hardware (e.g. from a table or something). I'm pretty sure that Android devices have an API for this since there are so many different devices out there. Even just hearing you confirm that this isn't possible for iOS has been useful -- thanks! – ETA May 01 '15 at 16:17
1

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}}

thorb65
  • 2,696
  • 2
  • 27
  • 37
  • Unfortunately this won't help in our case since (as mentioned above) we are running in non-native mode so the 5, 6, 6+ are all 320x568 devices for us. Your IS_IPHONE6PLUS and IS_IPHONE6 macros won't work for us (will always evaluate to FALSE) and your IS_IPHONE5 macro will evaluate to TRUE for anything but a 4S or smaller. – ETA May 01 '15 at 16:20
  • I guess I'd also argue that your approach has the same issue as using name=iPhone7,1 etc. to identify devices in that it could fail for future devices. It's probably not likely but what if the iPhone 7+ (if there is one) ends up having a height of < 736. I think this is where you are better off using size classes if possible (although the 5, 6, 6+ are all "regular" height in portrait -- the 6+ only differs in width in landscape where it is "regular" vs "compact" for the 5, 6)... – ETA May 01 '15 at 16:27
  • yes, thats right. since ios 8 size classes and layyoutconstraints are the better way. but what i dont really understand: if you run the apps in non native mode, why you need to know if a screen is larger? – thorb65 May 01 '15 at 16:31
  • On a larger screen (the iPhone 6+ for now) we insert an additional column into some of our table views and use smaller font sizes and that's still very readable. On a smaller screen (iPhone 6 and smaller for now), the 6+ UI is hard to read (and conversely, if we put the 6/5 UI onto a 6+ it looks too large). – ETA May 01 '15 at 17:43