ios - 如何准确检测设备型号?

标签 ios xcode model uidevice

我想检测用户拥有的iDevice,然后将设备名称放入UILabel。使用以下代码,应用程序仅检测 iPhone/iPad/iPod 我喜欢 iPhone 4/iPod 3G/iPad 1G...或确切名称(iPhone 3.1/iPod 2.0/iPad 2.4)...

这是我的代码:

iDevice.text = [UIDevice currentDevice]. localizedModel;

我试过了

iDevice.text = [UIDevice currentDevice]. model;

但小巷里说是 iPhone,我喜欢 iPhone 3.1

最佳答案

好的,听起来您想要使用的方法是使用位于 https://github.com/erica/uidevice-extension/ 的 Erica Sadun 创建的类别

在介绍如何使用它之前,我将介绍一些有关类别的信息。 Apple 在此处提供有关类别的文档 http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html

You can add methods to a class by declaring them in an interface file under a category name and defining them in an implementation file under the same name. The category name indicates that the methods are additions to a class declared elsewhere, not a new class. You cannot, however, use a category to add additional instance variables to a class.

从github下载项目,并将这两个文件添加到你的项目中:

UIDevice-Hardware.h
UIDevice-Hardware.m

您将使用的方法是以下方法之一:

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

因此,您需要将 UIDevice-Hardware.h 导入到您要使用该方法的文件中。您将使用该方法返回一个 NSString 值并将该值分配给一个标签,因此您将执行类似于

mylabel.text = [[UIDevice currentDevice] platformString]

这是另一个对类别有很好介绍的链接:http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/

编辑:使用设备模拟器的示例屏幕截图: enter image description here 注意:在我的@interface 行上方还有 #import "UIDevice-Hardware.h"

关于ios - 如何准确检测设备型号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13018097/

相关文章:

r - 在R中找到最佳线性模型的命令

sql - 在 SQL 中建模用户和角色的正确方法

ios - 将数据从模型传递到 Controller

ios - 使用旧版 Swift 语言版本”(SWIFT_VERSION)不起作用

ios - 登录用户使用 NSUserDefaults 执行 segue 不工作用户数据

iphone - 从 Dropbox 下载 sqlite 数据库

python - 需要帮助使用 SimPy 对火车系统进行建模

ios - 自定义卡片组应用程序的数据和类设计

iOS执行循环直到满足指定时间的特定条件?

ios - 通过 Interface Builder 为所有 View Controller 设置默认全局背景颜色