ios 检测IOS设备类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14372906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:53:24  来源:igfitidea点击:

To detect IOS device type

iphoneiosobjective-cipad

提问by GMsoF

I have found the solutions from here: Determine device (iPhone, iPod Touch) with iPhone SDK

我从这里找到了解决方案: 使用 iPhone SDK 确定设备(iPhone、iPod Touch)

From the link, it suggests to use the library https://gist.github.com/1323251

从链接中,它建议使用库https://gist.github.com/1323251

But obviously the library is quite outdated. I couldn't find the iPhone 5 and new iPad and etc in the list.

但显然图书馆已经过时了。我在列表中找不到 iPhone 5 和新 iPad 等。

Does anyone know how can I find the completed and updated list?

有谁知道我怎样才能找到完整和更新的列表?

Thank you so much.

非常感谢。

回答by Nitin Gohel

you can easily detect iphone, iphone5and iPadwith below condition:-

您可以轻松检测iphoneiphone5iPad具有以下条件:-

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


     }
     else
     {
         //iphone 3.5 inch screen
     }
 }
 else
 {
        //[ipad]
 }

my answer:- Detect device type

我的回答:- 检测设备类型

回答by fahrulazmi

Here's the updated version of https://gist.github.com/1323251. I'll keep it updated when new devices are released.

这是https://gist.github.com/1323251的更新版本。当新设备发布时,我会保持更新。

https://github.com/froztbytes/UIDeviceHardware

https://github.com/froztbytes/UIDeviceHardware

回答by rilar

This works just fine:

这工作得很好:

if([UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad) {
    NSLog(@"IPAD");
}else{
     NSLog(@"IPHONE");
}

回答by Mukund Agarwal

Just adding to @Mohammad Kamran Usmani answer. More specific iPhone types:

只是添加到@Mohammad Kamran Usmani 的答案。更具体的 iPhone 类型:

@import UIKit;

//Check which iPhone it is
double screenHeight = [[UIScreen mainScreen] bounds].size.height;
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
    NSLog(@"All iPads");
} else if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone)
{
    if(screenHeight == 480) {
        NSLog(@"iPhone 4/4S");
        smallFonts = true;
    } else if (screenHeight == 568) {
        NSLog(@"iPhone 5/5S/SE");
        smallFonts = true;
    } else if (screenHeight == 667) {
        NSLog(@"iPhone 6/6S");
    } else if (screenHeight == 736) {
        NSLog(@"iPhone 6+, 6S+");
    } else {
        NSLog(@"Others");
    }
}

回答by amar

Use the following code:

使用以下代码:

#import <sys/utsname.h>

- (NSString *)machineName
{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *temp = [NSString stringWithCString:systemInfo.machine
                                        encoding:NSUTF8StringEncoding];

    if ([temp rangeOfString:@"iPod"].location != NSNotFound)
    {
        return @"iPod";
    }

    if ([temp rangeOfString:@"iPad"].location != NSNotFound)
    {
        return @"iPad";
    }

    if ([temp rangeOfString:@"iPhone"].location != NSNotFound)
    {
        return @"iPhone";
    }

    return @"Unknown device";
}

回答by Mohd Kalimullah Sheikh

You can use the following code

您可以使用以下代码

if(screenSize.width==2048 && screenSize.height==1536)
{
   LetterParams.DeviceType=1;//IPadRetina
}
else if(screenSize.width==2048/2 && screenSize.height==1536/2)
{
   LetterParams.DeviceType=2;//IPad Non-Retina
}
else if(screenSize.width==1136 && screenSize.height==640)
{
   LetterParams.DeviceType=3;//IPhoneRetina
}
else
{
   LetterParams.DeviceType=4;//IPhone & Ipod
}

回答by Inder Kumar Rathore

I'm with other guys are maintaining the code on GitHubso please take the latest code from there. We're continuously adding new devices in the list.

我和其他人一起在GitHub 上维护代码,所以请从那里获取最新的代码。我们不断在列表中添加新设备。

Objective-C: GitHub/DeviceUtil

Objective-C: GitHub/DeviceUtil

Swift: GitHub/DeviceGuru

斯威夫特GitHub/DeviceGuru

#include <sys/types.h>
#include <sys/sysctl.h>

- (NSString*)hardwareDescription {
    NSString *hardware = [self hardwareString];
    if ([hardware isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
    if ([hardware isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    if ([hardware isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    if ([hardware isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    if ([hardware isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    if ([hardware isEqualToString:@"iPod1,1"]) return @"iPodTouch 1G";
    if ([hardware isEqualToString:@"iPod2,1"]) return @"iPodTouch 2G";
    if ([hardware isEqualToString:@"iPad1,1"]) return @"iPad";
    if ([hardware isEqualToString:@"iPad2,6"]) return @"iPad Mini";
    if ([hardware isEqualToString:@"iPad4,1"]) return @"iPad Air WIFI";
    //there are lots of other strings too, checkout the github repo
    //link is given at the top of this answer

    if ([hardware isEqualToString:@"i386"]) return @"Simulator";
    if ([hardware isEqualToString:@"x86_64"]) return @"Simulator";

    return nil;
}

- (NSString*)hardwareString {
    size_t size = 100;
    char *hw_machine = malloc(size);
    int name[] = {CTL_HW,HW_MACHINE};
    sysctl(name, 2, hw_machine, &size, NULL, 0);
    NSString *hardware = [NSString stringWithUTF8String:hw_machine];
    free(hw_machine);
    return hardware;
}

回答by Mohammad Kamran Usmani

if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)  
{
    NSLog(@"All iPads");  
}  
else
{
    else if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone)
    {
        if( screenHeight > 480 && screenHeight < 667 )
        {
            NSLog(@"iPhone 5/5s/6");
        }
        else if ( screenHeight > 480 && screenHeight < 736 )
        {
            NSLog(@"Other iPhones Resizable");
        }
        else if ( screenHeight > 480 )
        {
            NSLog(@"iPhone 6 +");
        }
        else
        {
            NSLog(@"iPhone 4s and others");
        }
    }

回答by i2097i

Here is a method that I came up with that focuses on key devices for screen measurement functions. It is a quick way to determine what you need. This will detect up to iPhone 5 and 5th Gen. iPod touches.

这是我想出的一种方法,它侧重于屏幕测量功能的关键设备。这是确定您需要什么的快速方法。这将检测最多 iPhone 5 和第 5 代 iPod touch。

typedef enum{
   iPadRetina,iPadNoRetina,iPhoneiPod35InchRetina,iPhoneiPod35InchNoRetina,iPhoneiPod4InchRetina}DeviceType;

-(void)yourCustomFunctionThatNeedsToKnowDeviceType
{
    NSLog(@"device type = %i",[self getDeviceType]);

    switch ([self getDeviceType])
    {
        case iPadRetina:
        {
            NSLog(@"This device is one of the following: iPad 3, iPad 4");
            break;
        }
        case iPadNoRetina:
        {
            NSLog(@"This device is one of the following: iPad 1, iPad 2, iPad mini");
            break;
        }
        case iPhoneiPod35InchRetina:
        {
            NSLog(@"This device is one of the following: iPhone 4/4S or iPod Touch 4th Generation");
            break;
        }
        case iPhoneiPod35InchNoRetina:
        {
            NSLog(@"This device is one of the following: iPhone 3G/3GS or iPod Touch 3rd Generation");
            break;
        }
        case iPhoneiPod4InchRetina:
        {
            NSLog(@"This device is one of the following: iPhone 5 or iPod Touch 5th Generation");
            break;
        }
    }
}

-(int)getDeviceType
{
    // Get the ratio of the device's screen (height/width)
    CGFloat screenRatio = [UIScreen mainScreen].bounds.size.height/[UIScreen mainScreen].bounds.size.width;

    // Initialize return value to negative value
    DeviceType type = -1;
    if(screenRatio > 1.5)
    {
        /*
            4.0-Inch Screen
            This implies that the device is either an iPhone 5 or a 5th generation iPod
            Retina display is implicit
         */
        type = iPhoneiPod4InchRetina;
    }
    else
    {
        /*
            Device must be iPad 1/2/3/4/mini or iPhone 4/4S or iPhone 3G/3GS
         */

        // Take a screenshot to determine if the device has retina display or not
        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *scaleCheckImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
        {
            /*
                Device must be iPad 1/2/3/4/mini
             */
            if(scaleCheckImage.scale == 1)
            {
                // iPad 1/2/mini (No Retina)
                type = iPadNoRetina;
            }
            else if(scaleCheckImage.scale == 2)
            {
                // iPad 3/4 (Retina)
                type = iPadRetina;
            }
        }
        else
        {
            /*
                Device must be iPhone 4/4S or iPhone 3G/3GS or iPod Touch 3rd Generation or iPod Touch 4th Generation
             */
            if(scaleCheckImage.scale == 1)
            {
                // iPhone 3G/3GS or iPod Touch 3rd Generation (No Retina)
                type = iPhoneiPod35InchNoRetina;
            }
            else if(scaleCheckImage.scale == 2)
            {
                // iPhone 4/4S or iPod Touch 4th Generation (Retina)
                type = iPhoneiPod35InchRetina;
            }
        }
    }
    return type;
}