我想检测用户拥有的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/
编辑:使用设备模拟器的示例屏幕截图:
注意:在我的@interface 行上方还有 #import "UIDevice-Hardware.h"
。
关于ios - 如何准确检测设备型号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13018097/