确定正在运行的设备类型应用程序

时间:2014-04-08 05:57:54

标签: ios

我是iOS开发的新手。我怎么知道我的应用程序当前安装的设备。我想要在iOS模拟器上检查当前设备是iPad还是iPhone的代码。

4 个答案:

答案 0 :(得分:0)

查找设备是iPad还是iPhone的最佳方法是

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
     // The device is an iPad running iPhone 3.2 or later.
}
else
{
     // The device is an iPhone or iPod touch.
}

如果您想了解有关哪种设备的详细信息,请参阅https://gist.github.com/Jaybles/1323251

答案 1 :(得分:0)

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
     // The device is an iPad
}
else
{
     // The device is an iPhone

     if(([[UIScreen mainScreen] bounds].size.height>520))
     {
        // The device is an iPhone5
     }
     else
     {
        // The device is an iPhone4
     }
}

答案 2 :(得分:0)

添加此代码可能有所帮助:

#define isiPhone5  ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE
#define isiPhone  (UI_USER_INTERFACE_IDIOM() == 0)?TRUE:FALSE

而不是用作

 if(isiPhone)
            {      

                if (isiPhone5)
                { 

                    //Code for iphone 5

                }
                else
                {
                    //iphone 3.5 inch screen

                }       
            }
            else
            {
                //[ipad]

            }

快乐编码

答案 3 :(得分:0)

使用此

+ (NSString *)yesButWhichDeviceIsIt
{
BOOL hasRetina = NO;
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
    CGFloat scale = [[UIScreen mainScreen] scale];
    if (scale > 1.0) {
        hasRetina = YES;
    }
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    if (hasRetina) {
        return @"iPad retina";
    } else {
        return @"iPad";
    }
 }
 else {
    if (hasRetina) {
        if ([[UIScreen mainScreen] bounds].size.height == 568){
            return @"iPhone5";
        }
        else
        {
            return @"iPhone4s";
        }
    } else {
        return @"iPhone";
    }
}
}

返回类型是String,它将为您提供设备类型

相关问题