在编码中识别不同的iOS设备? [英] Identify different iOS devices in coding?

查看:99
本文介绍了在编码中识别不同的iOS设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Objective-C中知道,有一种方法可以知道用户使用的设备.但是我确实是iOS开发的新手,我不知道Objective-C中那些复杂的命令.谁能告诉我如何通过编码识别手机型号.

I know the in objective-c, there is a way to know which device the user is using. But I really a new of iOS development, I do not know those complex and complicated command in objective-c. Could any one tell me how to identify the phone model by coding.

如果我能知道用户使用的是哪台设备,是否可以在不同的iOS设备(例如iPhone 3.5英寸和iPhone 4.0英寸)中以不同的字体大小更改UILabel?还是有什么方法可以在不同的iOS设备上更改字体位置(禁用自动布局)?

Also, if I can know which device the user is using, is that possible to change the UILabel in different font size in different iOS device, such as iPhone 3.5" and iPhone 4.0"? Or is there any way to change the font position in different iOS device (autolayout is disabled)?

推荐答案

我确定正在运行的iOS设备的方式,因此我们可以根据iDevices的大小(例如iPadiPhone)更改布局.

The way I determine what iOS device I am running on so we can change layout based on the iDevices size such as iPad and iPhone is like.

   // iPhone 5 (iPhone 4")
   #define IS_PHONEPOD5() ([UIScreen mainScreen].bounds.size.height == 568.0f && [UIScreen mainScreen].scale == 2.f && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

要获取iPhone 5,您也可以这样做

To get iPhone 5 you could also do

  // iPhone 5 alternative
  #define IS_PHONEPOD5() ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

  // iPad
  #define IS_IPAD() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

  // iPhone 4 (iPhone 3.5")
  #define IS_IPHONE() (UI_USER_INTERFACE_IDIOM() == UIUserIntefaceIdiomPhone)

我将它们添加到我的xxxx-Prefix.pch中,这样我就可以在整个项目中使用它,而不必执行#import#include.我并不是说这是最好的方法,但是这种方法对我来说非常合适.

I add these into my xxxx-Prefix.pch so that I can use it throughout my project without having to do an #import or #include. I am not saying this is the best way but this way works perfectly fine for me.

如果设置了这些变量,则可以使用if statements之类的

If you set these variables you can then just use if statements like

  if(IS_IPAD()) {
      // Do something if iPad
  } else if(IS_IPHONEPOD5()) {
      // Do something if iPhone 5 or iPod 5
  } else {
     // Do something for everything else (This is what I do) but you could 
     // replace the 'else' with 'else if(IS_IPHONE())'

  }

不过,还有其他方法可以做到.例如,一个开发人员编写了一些扩展UIDevice的代码,可以在 https://github.com中找到/erica/uidevice-extension/

There are other ways of doing it though. Such as one developer has written some code that extends UIDevice this can be found https://github.com/erica/uidevice-extension/

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

这将使您能够检测iDevice是否为iPhone 33GS4等.

This will allow you to detect whether the iDevice is an iPhone 3, 3GS, 4 and so on.

如果您有任何问题,请在评论中提问,我将改善我的回答.希望这里有

If you have any questions please just ask in the comments and I will improve my answer to include. Hope it helps if it has here is a google search for determining iDevice

这篇关于在编码中识别不同的iOS设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆