is iPhone 5 always Returns False [closed] - iphone

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am using the following code (in AppDelegate) to detect if the device is iPhone 5
bool isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136));
it returns false always. this is not the first time I used that code. eventhe NSLog for that returns {320, 480}
NSLog(#"%#",NSStringFromCGSize([[UIScreen mainScreen] bounds].size));
NOTE: The app was for iPad only and then I made it universal. so I will have 2 storyBoards that is why I need the detection code.
Thanks

This should work mate,
bool isiPhone5 = ([[UIScreen mainScreen] bounds].size.height == 568);
and make sure you use 4-inch simulator

I used the following code. You can try this:
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
Then you can do:
if (IS_IPHONE_5) {
//Do your stuff
}
Hope this helps.

#define IS_iPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
if(IS_iPHONE_5)
{
//Do something
}
else
{
}

To identify iphone 5 try this
if( [[UIScreen mainScreen]bounds].size.height == 568){
//iphone 5
}
else
{
// less than iphone 5
}

try this
#include <sys/sysctl.h>
-(NSString *)getModel {
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *model = malloc(size);
sysctlbyname("hw.machine", model, &size, NULL, 0);
NSString *deviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
free(model);
return deviceModel;
}
this Link should help

See Below One
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone){
if( [[UIScreen mainScreen]bounds].size.height == 568){
//iPhone5 Device
}
else
{
// Normal iPhone device
}
}
EDIT: see same thread

Finally I figure it out.
Solution
Because the App was for iPad only the Default-568h#2x.png doesn't exist in the project. so after adding it all work properly.
Note and I dont need the above code because after making the app universal in the xcode project setting there is a part for iPhone/iPod Deployment info configurations (icons,mainStoryBorad,default screens ....) where I can setup the iPhone5 storybord after adding it to the project.

Related

identify iPhone 3, 4 and 5 in the same #define

I've been using the next line in my constants
to differentiate between devices and get back the number of the device.
What's the appropriate way to identify iPhone 5 and still keep it in a one line format?
#define iPhoneType [[UIScreen mainScreen] scale]==2 || [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad ? #"4" : #"3"
Thanks
Edit: A lot of good answers but my goal is to keep it in a one line format for all devices.
Edit:
Based on the comments, this question needs some clarification. Here are the requirements:
A single-line macro that returns either #"3", #"4", or #"5" depending on the iOS device.
The 4" devices (currently iPhone 5 and 5th gen iPod touch) should return #"5".
All iPads and all remaining retina iPhones and iPod touches should return #"4".
All remaining non-retina iPhones and iPod touches should return #"3".
According to your question I'm assuming you want to identify the hardware device, not the iOS version.
/*
Erica Sadun, http://ericasadun.com
iPhone Developer's Cookbook, 6.x Edition
BSD License, Use at your own risk
*/
#include <sys/sysctl.h>
NSString* getSysInfoByName(char* typeSpecifier) {
size_t size;
sysctlbyname(typeSpecifier, NULL, &size, NULL, 0);
char *answer = malloc(size);
sysctlbyname(typeSpecifier, answer, &size, NULL, 0);
NSString *results = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];
free(answer);
return results;
}
NSString* platform() {
return getSysInfoByName("hw.machine");
}
Import those functions in the .pch, then you are free to call this one liner:
BOOL isIphone5 = [platform() hasPrefix:#"iPhone5"];
It works for any device. See UIDevice-Hardware.m for a list of the strings returned.
Assuming the updated requirements are correct, the following should work:
#define iPhoneType (fabs((double)[UIScreen mainScreen].bounds.size.height - (double)568) < DBL_EPSILON) ? #"5" : ([UIScreen mainScreen].scale==2 || UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? #"4" : #"3")
This will return #"5" for the 4" screened iPhones and iPod touches. This will return #"4" for all iPads and retina iPhones and iPod touches. And it will return #"3" for non-retina iPhones and iPod touches.
Define Following Constants in .pch file of your project
#define IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.width >= 568 || [[UIScreen mainScreen] bounds].size.height >= 568)?YES:NO
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)?YES:NO
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)?YES:NO
#define DeviceType ((IS_IPAD)?#"IPAD":(IS_IPHONE5)?#"IPHONE 5":#"IPHONE")
Now Check device type
NSLog(#"%# %#",DeviceType,[DeviceType isEqualToString:#"IPAD"]?#"YES":#"NO");
Use following sequence to Identify Device Type
if(IS_IPAD)
NSLog(#"IPAD");
else if(IS_IPHONE5)
NSLog(#"IPHONE 5");
else
NSLog(#"IPHONE");
The best way to identify the different iOS devices programatically is their screen resolution. I done same in my application it is working great. Please refer my code.
- (NSString *) getDeviceScreenWidth
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screenBounds);
NSNumber* number = [NSNumber numberWithFloat:width];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:kCFNumberFormatterDecimalStyle];
NSString* commaString = [numberFormatter stringForObjectValue:number];
NSString *screenWidth = [NSString stringWithFormat:#"%#",commaString];
NSLog(#"screen Width is: %#",screenWidth);
return screenWidth;
}
- (NSString *) getDeviceScreenHeight
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat height = CGRectGetHeight(screenBounds);
NSNumber* number = [NSNumber numberWithFloat:height];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:kCFNumberFormatterDecimalStyle];
NSString* commaString = [numberFormatter stringForObjectValue:number];
NSString *screenHeight = [NSString stringWithFormat:#"%#",commaString];
NSLog(#"screen height is: %#",screenHeight);
return screenHeight;
}
As sample you may found this code in cocos2d framework:
-(NSInteger) runningDevice
{
NSInteger ret=-1;
#ifdef __CC_PLATFORM_IOS
if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
ret = (CC_CONTENT_SCALE_FACTOR() == 2) ? kCCDeviceiPadRetinaDisplay : kCCDeviceiPad;
}
else if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone )
{
// From http://stackoverflow.com/a/12535566
BOOL isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136));
if( CC_CONTENT_SCALE_FACTOR() == 2 ) {
ret = isiPhone5 ? kCCDeviceiPhone5RetinaDisplay : kCCDeviceiPhoneRetinaDisplay;
} else
ret = isiPhone5 ? kCCDeviceiPhone5 : kCCDeviceiPhone;
}
#elif defined(__CC_PLATFORM_MAC)
// XXX: Add here support for Mac Retina Display
ret = kCCDeviceMac;
#endif // __CC_PLATFORM_MAC
return ret;
}
I hope this code helps change your macro.

How to check the device is iPhone 5? [duplicate]

This question already has answers here:
How to detect iPhone 5 (widescreen devices)?
(24 answers)
Closed 9 years ago.
How can I check if an application running on iPhone 5 or not and then do something?
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
then in the code:
if (IS_IPHONE_5) {
//is iphone 5
}
You're likely concerned with the window size, not the make/model, this will do:
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
if (screenRect.size.height == 568)
{
// this is an iPhone 5+
}
You can do it with checking screen resolution or you can do it with using:
#import "sys/utsname.h"
which give you identifier for each device. Just see my answer here: recognize device
Using screen is fine.
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)
No epsilon check is required
We can directly put this method in constant file and can use any where using define
#define ISIPHONE5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
#define HEIGHT (ISIPHONE5 ? 60:145)
Or can use
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
We can also check IOS via below
#define IOS_OLDER_THAN_6 ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0 )
#define IOS_NEWER_OR_EQUAL_TO_6 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0 )
You can check your device's iOS version if is 6.0 then its a iPhone 5.....
Here's the code..
double osVersion = [[[UIDevice currentDevice] systemVersion] doubleValue];
NSLog(#"OSVersion: %f", osVersion);
if (osVersion == 6.0)
{
//Paste your code here.....
}

Best way to detect the device is iPhone 5 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to detect iPhone 5 (widescreen devices)?
Does anybody know a better way to detect whether the device is an iPhone 5 than checking the screen height?
[UIScreen mainScreen].bounds.size.height == 568.0;
Thanks in advance.
I use the following macro:
#define IS_IPHONE ( [[[UIDevice currentDevice] model] isEqualToString:#"iPhone"] )
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
#define IS_IPHONE_5 ( IS_IPHONE && IS_HEIGHT_GTE_568 )
And then i can do:
if(IS_IPHONE_5)
{
NSLog(#"i am an iPhone 5!");
}
else
{
NSLog(#"This is not an iPhone 5");
}

Detect iphone 5 4" screen [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to detect iPhone 5 (widescreen devices)?
Is there a way I can detect if the current device is the iphone 5? More specifically if it's using the new 4" screen?
Use this
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
I think you should concentrate on preferred display mode, rather than detecting iPhone5. Who knows what devices Apple will manufacture, but if your software supports that mode, it will be futureproof.
BOOL isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136));
In the future, folks might want to change preferred display mode on the fly. For example disconnect AppleTV from 720p tv and plug to 1080p, without restarting the app of course.
Add this code in your initializtion:
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if(UIScreenOverscanCompensationScale==1136/640){
//move to your iphone5 storyboard
[UIStoryboard storyboardWithName:(NSString *) bundle (NSBundle *)];
}
else{
//move to your iphone4s storyboard
[UIStoryboard storyboardWithName:(NSString *) bundle (NSBundle *)];
}
}
This was an answer posted by me in another question here.
Add this code to your application:
if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f)
{// iPhone 5 code}
else
{// previous version code}

Check iOS version at runtime?

This is sort of a follow on from my last question. I am using beginAnimations:context: to setup an animation block to animate some UITextLabels. However I noticed in the docs that is says: "Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods instead."
My question is I would love to use animateWithDuration:animations: (available in iOS 4.0 and later) but do not want to exclude folks using iOS 3.0. Is there a way to check to iOS version of a device at runtime so that I can make a decision as to which statement to use?
Simpler solution for anyone who'll need help in the future:
NSArray *versionCompatibility = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
if ( 5 == [[versionCompatibility objectAtIndex:0] intValue] ) { /// iOS5 is installed
// Put iOS-5 code here
} else { /// iOS4 is installed
// Put iOS-4 code here
}
In many cases you do not need to check iOS version directly, instead of that you can check whether particular method is present in runtime or not.
In your case you can do the following:
if ([[UIView class] respondsToSelector:#selector(animateWithDuration:animations:)]){
// animate using blocks
}
else {
// animate the "old way"
}
to conform to version specified in system defines
//#define __IPHONE_2_0 20000
//#define __IPHONE_2_1 20100
//#define __IPHONE_2_2 20200
//#define __IPHONE_3_0 30000
//#define __IPHONE_3_1 30100
//#define __IPHONE_3_2 30200
//#define __IPHONE_4_0 40000
You can write function like this
( you should probably store this version somewhere rather than calculate it each time ):
+ (NSInteger) getSystemVersionAsAnInteger{
int index = 0;
NSInteger version = 0;
NSArray* digits = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
NSEnumerator* enumer = [digits objectEnumerator];
NSString* number;
while (number = [enumer nextObject]) {
if (index>2) {
break;
}
NSInteger multipler = powf(100, 2-index);
version += [number intValue]*multipler;
index++;
}
return version;
}
Then you can use this as follows:
if([Toolbox getSystemVersionAsAnInteger] >= __IPHONE_4_0)
{
//blocks
} else
{
//oldstyle
}
Xcode 7 added the available syntax making this relatively more simple:
Swift:
if #available(iOS 9, *) {
// iOS 9 only code
}
else {
// Fallback on earlier versions
}
Xcode 9 also added this syntax to Objective-C
Objective-C:
if (#available(iOS 9.0, *)) {
// iOS 9 only code
} else {
// Fallback on earlier versions
}
Most of these solutions on here are so overkill. All you need to do is [[UIDevice currentDevice].systemVersion intValue]. This automatically removes the decimal, so there is no need to split the string.
So you can just check it like:
if ([[UIDevice currentDevice].systemVersion intValue] >= 8) {
// iOS 8.0 and above
} else {
// Anything less than iOS 8.0
}
You can also define a macro with this code:
#define IOS_VERSION [[UIDevice currentDevice].systemVersion intValue];
or even include your check:
#define IOS_8PLUS ([[UIDevice currentDevice].systemVersion intValue] >= 8)
Then you just need to do:
if (IOS_8PLUS) {
// iOS 8.0 and above
} else {
// Anything less than iOS 8.0
}
Discouraged is not the same as deprecated.
If you need to support earlier versions of iOS that do not have the block based methods, there is nothing wrong with using the older methods (as long as they haven't been removed, of course).
You can use the version of the Foundation framework to determine the current system version.
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1){
//for earlier versions
} else {
//for iOS 7
}
For my purposes I've written a tiny library that abstracts away the underlying C calls and presents an Objective-C interface.
GBDeviceDetails deviceDetails = [GBDeviceInfo deviceDetails];
if (deviceDetails.iOSVersion >= 6) {
NSLog(#"It's running at least iOS 6"); //It's running at least iOS 6
}
Apart from getting the current iOS version, it also detects the hardware of the underlying device, and gets info about the screen size; all at runtime.
It's on github: GBDeviceInfo. Licensed under Apache 2.
Put this in your Prefix.pch file
#define IOS_VERSION [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:#"."] firstObject] intValue]
And then you can check iOS versions like:
if(IOS_VERSION == 8)
{
// Hello 8!
}
else
{
// Hello some other version!
}
Off course if you can use feature detection (and it makes sense for your use case) you should do that.
In MonoTouch:
To get the Major version use:
UIDevice.CurrentDevice.SystemVersion.Split('.')[0]
For minor version use:
UIDevice.CurrentDevice.SystemVersion.Split('.')[1]
A bit nicer and more efficient adaptation to the above solutions:
-(CGPoint)getOsVersion
{
static CGPoint rc = {-1,-1};
if (rc.x == -1) {
NSArray *versionCompatibility = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
rc.x = [versionCompatibility[0] intValue];
rc.y = [versionCompatibility[1] intValue];
}
return rc;
}
now you can
if ([self getOsVersion].x < 7) {
}
HTH