加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 如何准确检测iDevice模型?

发布时间:2020-12-14 17:28:56 所属栏目:百科 来源:网络整理
导读:我想检测用户的iDevice,然后将设备名称放在UILabel中.使用以下代码,应用程序仅检测iPhone / iPad / iPod我喜欢iPhone 4 / iPod 3G / iPad 1G …或确切名称(iPhone 3.1 / iPod 2.0 / iPad 2.4)… 这是我的代码: iDevice.text = [UIDevice currentDevice]. lo
我想检测用户的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

解决方法

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

在我开始使用它之前,我将传递一些关于类别的信息. 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”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读