首页 > Web开发 > 详细

iOS中判断设备类型的方法

时间:2014-01-14 19:27:32      阅读:693      评论:0      收藏:0      [点我收藏+]

在iOS开发上,有时避免不了要做一些设备类型的判断,比如iPhone、iPad。

 

iOS也给我们提供了几个方法,用来判断这些内容。

文档中公开的主要有三个属性(property),都在UIDevice里面。

bubuko.com,布布扣
@property(nonatomic, readonly, retain) NSString *model;

@property(nonatomic, readonly) UIUserInterfaceIdiom userInterfaceIdiom;

@property(nonatomic, readonly, retain) NSString *localizedModel;
bubuko.com,布布扣

 

其中licalizedModel与model貌似结果都一样的....不知道。本人亲自用iPhone试了换成日文,区域为日本等,这两个返回的都是@“iPhone”。网上google了下,其他人遇到的情况也是这样

所以下面着重讨论model和userInterfaceIdiom这两个接口。

 

userInterfaceIdiom

这个属性,苹果文档说的很明白,是返回一个枚举,而且只有两个值,那就是:

UIUserInterfaceIdiomPhone
UIUserInterfaceIdiomPad

毫无疑问,iPhone/Universal App在iPhone(包括模拟器)上运行,得到的肯定是UIUserInterfaceIdiomPhone, iPad/Universal App在iPad(包括模拟器)上运行,得到的是UIUserInterfaceIdiomPad。

但是如果一个iPhone App(不是Universal)在iPad(或者模拟器)上运行的结果又是什么呢?可能很多人猜错了。答案是UIUserInterfaceIdiomPhone。

那是为什么呢?原来iPhone App兼容iPad并在iPad上运行的时候,它的屏幕尺寸是和3.5寸屏大小是一致的。它采用的启动图片也与3.5寸屏相同

我们可以验证下,

设备:iPad

系统 : iOS6,iOS7

然后采用下面的代码去创建window:

bubuko.com,布布扣
CGRect screenBounds = [[UIScreen mainScreen] bounds]; 
    
self.window = [[UIWindow alloc] initWithFrame:screenBounds];
bubuko.com,布布扣

结果: screenBounds的大小是320*480。

ps.  iPad App是不能在iPhone上运行的。

 

Model

苹果文档描述是返回一个字符串,比如@"iPhone"、@“iPad”。

这个接口没有什么异议,网上也有很多文章说了它的作用。它可以得到以下字符串:

@"iPhone"
@"iPad"
@"iPod touch"
@"iPhone Simulator"
@"iPad Simulator"

iOS中判断设备类型的方法

原文:http://www.cnblogs.com/perryxiong/p/3512290.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!