0

does anyone know an alternative to the following code? I want to support iOS 7 and below in my app and .nativeScale does not work on these firmwares. I could not find a solution on the internet that's why I am asking here. (normally letting screenbounds check for height of 568 does not work for iPhone 6+)

The Code I'm referring to:

CGRect screenBounds = [[UIScreen mainScreen] bounds];
if ([UIScreen mainScreen].nativeScale > 2.1) {
//6 plus

} else if (screenBounds.size.height == 568) {
//4 inch / iPhone 6 code

} else {
//3.5 inch code

}

Thanks in advance

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
  • What's your goal? No iPhone 6/6+ will be running iOS 7. And why do you claim that a height of 568 equates to an iPhone 6? That's the height for the iPhone 5, not the iPhone 6 (unless your app is scaling). – rmaddy Oct 18 '14 at 05:21
  • My goal is to support iOS 7 and 6 if possible. Right now my app crashes (on my iPhone 4S - 7.1.1) because .nativeScale is not compatible below iOS 8. I meant exactly what you said, 568 does not work for iPhone 6+ which is the reason for why I had to implement the second method (.nativeScale) – LinusGeffarth Oct 18 '14 at 05:24
  • 1
    How about: `if ([[UIScreen mainScreen] respondsToSelector:@selector(nativeScale)]) { .. ios 8 code .. }` – Abhi Beckert Oct 18 '14 at 05:32
  • Does your app fully support native resolution of the iPhone 6 and 6+ (in other words, do you have proper iPhone 6/6+ launch images)? Or does your app only support 3.5 and 4" devices and the app is scaled on the iPhone 6 and 6+? If the former, check for the proper sizes. If the latter, why do you need to know if the app is on an iPhone 6 or 6+? It won't matter. – rmaddy Oct 18 '14 at 05:34
  • I already fixed it. Thanks anyway. I will post a solution if you are interested – LinusGeffarth Oct 18 '14 at 05:37
  • @AbhiBeckert thank you again for your hint, if you look at my answer it really helped me! (that's why I voted it up) – LinusGeffarth Oct 18 '14 at 06:15

1 Answers1

4

So what I did was: First I had the methods in the following order:

if ([UIScreen mainScreen].nativeScale > 2.1) {
    //6 plus

} else if (screenBounds.size.height == 568) {
    //4 inch code

} else {
//3.5 inch code

}

Then I thought since the computer stops running the if else statement once he finds a true one I just rearranged the order to the following:

if (screenBounds.size.height == 480) {
//3.5 inch code

} else if ([UIScreen mainScreen].nativeScale > 2.1) {
    //6 plus

} else if (screenBounds.size.height == 568) {
    //4 inch code

}

This supported an iPhone 4S with iOS 7 or below. On iPhone 5 / 5S it would still crash. That's why I changed it in the end to the following:

if ([[UIScreen mainScreen] respondsToSelector:@selector(nativeScale)]) {
//checks if device is running on iOS 8, skips if not
    NSLog(@"iOS 8 device");

    if (screenBounds.size.height == 480) {
        //3.5 inch code
        NSLog(@"iPhone 4S detected");

    } else if ([UIScreen mainScreen].nativeScale > 2.1) {
        //6 plus
        NSLog(@"iPhone 6 plus detected");

    } else if (screenBounds.size.height == 568) {
        //4 inch code
        NSLog(@"iPhone 5 / 5S / 6 detected");
    }
} else if (screenBounds.size.height == 480) {
    //checks if device is tunning iOS 7 or below if not iOS 8
    NSLog(@"iOS 7- device");
    NSLog(@"iPhone 4S detected");
//3.5 inch code

} else if (screenBounds.size.height == 568) {
    NSLog(@"iOS 7- device");
    NSLog(@"iPhone 5 / 5S / 6 detected");
    //4 inch code

}

It should now fully work on any iOS 7 an iOS 8 device!

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174