如何在iOS中查看iPhone设备版本?

时间:2012-09-23 13:46:26

标签: ios iphone-5

您好我想在iOS中查看iPhone设备版本。

我的意思是,目前正在运行的设备是iPhone 4或iPhone 5。

我需要查看设备,是不是iPhone 5?

因为我的应用程序中有一些问题需要知道iPhone 5。

那我该怎么办?

5 个答案:

答案 0 :(得分:9)

添加此代码:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
        if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)]) {
            CGSize result = [[UIScreen mainScreen] bounds].size;
            CGFloat scale = [UIScreen mainScreen].scale;
            result = CGSizeMake(result.width * scale, result.height * scale);

            if(result.height == 960) {
                NSLog(@"iPhone 4 Resolution");
                resolution_number = 1;
            }
            if(result.height == 1136) {
                NSLog(@"iPhone 5 Resolution");
            }
        }
        else{
            NSLog(@"Standard Resolution");
        }
    }

答案 1 :(得分:9)

将此宏添加到您的代码中:

#define HEIGHT_IPHONE_5 568
#define IS_IPHONE   ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 ([[UIScreen mainScreen] bounds ].size.height == HEIGHT_IPHONE_5 )

然后随时检查..

if (IS_IPHONE_5) {
    //Code for iPhone5
}else{
    //Code for earlier version
}

答案 2 :(得分:5)

根据此question的答案,您可以确定您使用的是哪种型号 通过这个:

NSString *deviceModel = [UIDevice currentDevice].model;

结果可以是其中一个值:iPod touch,iPhone,iPad,iPhone模拟器,iPad模拟器

对于特定型号,您可以通过在UIDevice.h类上创建类别来确定您使用的版本:

的UIDevice + Utilities.h

#import <UIKit/UIKit.h>

@interface UIDevice (Utilities)

- (CGFloat)deviceModelVersion;

@end

的UIDevice + Utilities.m

#import "UIDevice+Utilities.h"
#include <sys/types.h>
#include <sys/sysctl.h>

@implementation UIDevice (Utilities)

- (CGFloat)deviceModelVersion
{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithUTF8String:machine];
    free(machine);

    if ([platform rangeOfString:@"iPhone"].location != NSNotFound)
    {
        return [[[platform stringByReplacingOccurrencesOfString:@"iPhone" withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue];
    }
    else if ([platform rangeOfString:@"iPad"].location != NSNotFound)
    {
        return [[[platform stringByReplacingOccurrencesOfString:@"iPad" withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue];
    }
    else if ([platform rangeOfString:@"iPod"].location != NSNotFound)
    {
        return [[[platform stringByReplacingOccurrencesOfString:@"iPod" withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue];
    }
    else if ([platform rangeOfString:@"i386"].location != NSNotFound || [platform rangeOfString:@"x86_64"].location != NSNotFound)
    {
        return -1.0; //Currently it is not possible (or maybe it is, but I do not know)
                     //which type of simulator device model version your app is running
                     //so I am returning -1.0 device model version for all simulators types
    }

    return 0.0;
}

@end

如何调用函数deviceModelVersion的示例:

CGFloat deviceModelVersion = [UIDevice currentDevice].deviceModelVersion;

可能的结果可能是1.0,1.1,......,2.0,2.1,......,3.0,3.1,..,4.0,4.1,..,5.0,5.1 ......

要确定它是否为iPhone 5,您将拥有

deviceModel : "iPhone" 

deviceModelVersion : >=5.0 and < 6.0

答案 3 :(得分:1)

我实际上找到了一个#define来实现这个技巧

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

答案 4 :(得分:0)

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_4 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0f)
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)
#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0f)
#define IS_IPHONE_6PLUS (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f)
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0f)
#define IS_RETINA ([[UIScreen mainScreen] scale] == 2.0f)