Question

I have used this code to detect if the device is iPhone 5 or not to set appropriate UI.

( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

However, it returns a 0 even if the app is running in iPhone 5c. I also referred to How to detect iPhone 5 (widescreen devices)? but still the updated code doesnt work. Please help.

Was it helpful?

Solution

Well, at first you do also recognize iPhone5S as iPhone 5, which is not the same. I would rather make use of the the device name, as your macros are expandable as much as you like.

#import <sys/utsname.h>
NSString* deviceName()
{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *result = [NSString stringWithCString:systemInfo.machine
                                          encoding:NSUTF8StringEncoding];
    return result;
}

#define isIPhone5  [deviceName() rangeOfString:@"iPhone5,"].location != NSNotFound
#define isIPhone5S [deviceName() rangeOfString:@"iPhone6,"].location != NSNotFound

The list of the devices is here:

/*
 @"i386"      on the simulator
 @"iPod1,1"   on iPod Touch
 @"iPod2,1"   on iPod Touch Second Generation
 @"iPod3,1"   on iPod Touch Third Generation
 @"iPod4,1"   on iPod Touch Fourth Generation
 @"iPod5,1"   on iPod Touch Fifth Generation
 @"iPhone1,1" on iPhone
 @"iPhone1,2" on iPhone 3G
 @"iPhone2,1" on iPhone 3GS
 @"iPad1,1"   on iPad
 @"iPad2,1"   on iPad 2
 @"iPad3,1"   on 3rd Generation iPad
 @"iPad3,2":  on iPad 3(GSM+CDMA)
 @"iPad3,3":  on iPad 3(GSM)
 @"iPad3,4":  on iPad 4(WiFi)
 @"iPad3,5":  on iPad 4(GSM)
 @"iPad3,6":  on iPad 4(GSM+CDMA)
 @"iPhone3,1" on iPhone 4
 @"iPhone4,1" on iPhone 4S
 @"iPhone5,1" on iPhone 5
 @"iPad3,4"   on 4th Generation iPad
 @"iPad2,5"   on iPad Mini
 @"iPhone5,1" on iPhone 5(GSM)
 @"iPhone5,2" on iPhone 5(GSM+CDMA)
 @"iPhone5,3  on iPhone 5c(GSM)
 @"iPhone5,4" on iPhone 5c(GSM+CDMA)
 @"iPhone6,1" on iPhone 5s(GSM)
 @"iPhone6,2" on iPhone 5s(GSM+CDMA)
 */

My approach is related to the post here, but can be easily used in prefix header: Detect if the device is iPhone 5s


To make sure you can use this macro in each file, follow these steps: create a new file (lets call it "Helperfunctions")

The .h file contains the definition of the function, nothing else:

NSString* deviceName();

The .m file contains the given deviceName-code:

#import "Helperfunctions.h"
#import <sys/utsname.h>

NSString* deviceName()
{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *result = [NSString stringWithCString:systemInfo.machine
                                          encoding:NSUTF8StringEncoding];
    return result;
}

In the precompiled header file you could add now the code (or to another file, you already imported in the .pch, so it looks like:

//
//  Prefix header
//
//  The contents of this file are implicitly included at the beginning of every source file.
//

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>

    #import "Helperfunctions.h"

    #define isIPhone5  [deviceName() rangeOfString:@"iPhone5,"].location != NSNotFound
    #define isIPhone5S [deviceName() rangeOfString:@"iPhone6,"].location != NSNotFound
#endif

OTHER TIPS

Alternatively, if you wanted something similar to what you were already doing, why not just:

BOOL iPhone5 = [[ UIScreen mainScreen ] bounds ].size.height >= 568 ;

Does your app have an R4 Launch Image (if you’re using an asset catalog) or a Default-568h@2x.png launch image? If not, it will think it is running on a 3.5ʺ screen no matter what device it is actually running on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top