如何通过编程识别iPhone(3G,4S,5)的版本

4

这个链接可能会有所帮助...https://dev59.com/0l7Va4cB1Zd3GeqPGRJQ - rptwsthi
6个回答

12

您可以根据以下条件轻松检测iPhone 3GS-4S、iPhone 5和iPad:

 if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
 {
     if ([[UIScreen mainScreen] bounds].size.height == 568)
     {

          //iphone 5
     }
     else
     {
         //iphone 3.5 inch screen iphone 3g,4s 
     }
 }
 else
 {
        //[ipad]
 }

请访问此处检测设备类型的答案。


2
我认为这个答案应该更新,因为我们现在有iPhone 6和6+。 - Miknash

6
请使用以下代码。
#import <sys/utsname.h>

NSString* machineName()
{
    struct utsname systemInfo;
    uname(&systemInfo);

    return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}

使用以下代码来识别您的设备类型。
if([machineName() isEqualToString:@"iPhone1,2"])
{
    deviceType = @"iPhone 3G";
}
else if([machineName() isEqualToString:@"iPhone2,1"])
{
    deviceType = @"iPhone 3GS";
}
else if([machineName() isEqualToString:@"iPhone3,1"] || [machineName() isEqualToString:@"iPhone3,2"] || [machineName() isEqualToString:@"iPhone3,3"])
{
    deviceType = @"iPhone 4";
}
else if([machineName() isEqualToString:@"iPhone4,1"])
{
    deviceType = @"iPhone 4S";
}
else if([machineName() isEqualToString:@"iPhone5,1"])
{
    deviceType = @"iPhone 5";
}
else if([machineName() isEqualToString:@"iPod1,1"])
{
    deviceType = @"iPod Touch 1G";
}
else if([machineName() isEqualToString:@"iPod2,1"] || [machineName() isEqualToString:@"iPod2,2"])
{
    deviceType = @"iPod Touch 2G";
}
else if([machineName() isEqualToString:@"iPod3,1"])
{
    deviceType = @"iPod Touch 3G";
}
else if([machineName() isEqualToString:@"iPod4,1"])
{
    deviceType = @"iPod Touch 4G";
}
else if([machineName() isEqualToString:@"iPod5,1"])
{
    deviceType = @"iPod Touch 5G";
}

4
您可以使用UIDevice(硬件)类别,它是由Erica Sadun开发的UIDevice扩展
它可用于以下方面:
[[UIDevice currentDevice] platformType]   // ex: UIDevice4GiPhone

[[UIDevice currentDevice] platformString] // ex: @"iPhone 4G"

你尝试过这段代码吗? - user1881612
当我使用这段代码时,它会给我一个警告:"未找到实例方法 +platformString"。 - user1881612
您需要导入类别的 .h 文件。 - Akshay Shah

2
您可以使用以下代码来实现此功能:
NSString *device()
{
    struct utsname devInfo;
    uname(&devInfo);

    return [NSString stringWithCString:devInfo.machine encoding:NSUTF8StringEncoding];
}

请确保您导入了 #import <sys/utsname.h>


1

您好,请使用以下代码

[UIDevice currentDevice] platformType]   // returns UIDevice4GiPhone
[[UIDevice currentDevice] platformString] // returns @"iPhone 4G"

或者请检查ratina

+ (BOOL) isRetina
{
    if([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        return [[UIScreen mainScreen] scale] == 2.0 ? YES : NO;

    return NO;
}

或者检查 iOS 版本

   + (BOOL) isIOS5
    {
        NSString *os5 = @"5.0";
        NSString *currSysVer = [[UIDevice currentDevice] systemVersion];

        //    currSysVer = @"5.0.1";
        if ([currSysVer compare:os5 options:NSNumericSearch] == NSOrderedAscending) //lower than 4
        {
            return NO;
        }
        else if ([currSysVer compare:os5 options:NSNumericSearch] == NSOrderedDescending) //5.0.1 and above
        {        
            return YES;
        }
        else // IOS 5
        {
            return YES;
        }

        return NO;
    }

UIDeviceš▒╗ńŞşŠ▓튝ëplatformStringŠłľplatformTypeŠľ╣Š│ĽŃÇé - Midhun MP

0

您可以使用uidevice-extension获取这些内容。 您需要的是以下功能:

- (NSString *) platform;
- (NSString *) hwmodel;
- (NSUInteger) platformType;
- (NSString *) platformString;

来自UIDevice-Hardware.h


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接