在运行时确定 iPhone 设备
有没有办法在运行时对iPhone设备进行运行时检查? 它必须能够将 iPhone 4 与其他 iPhone/iPod touch 型号区分开来。 任何具有相同功能的解决方法都可以。
Is ther any way to perform a runtime check for the iPhone device at runtime?
It has to be able to differenciate iPhone 4 from other iPhone/iPod touch models.
Any workaround that does the same thing is OK too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用类似这样的代码来达到相同的目的:
只有 iOS 4.x+ 设备支持 UIScreen
scale
实例方法。由于 iPhone4 不运行 iOS 3,我们可以立即排除这些可能性。接下来,我们检查比例因子是否为 2.0,如果是,我们就知道它有视网膜显示屏。虽然这还不确定(苹果明天可能会发布另一款视网膜设备),但它确实在重要的地方测试了“模型”——即,您可以从提供 @2x 图像和标准图像的 Web 服务中获取图像,这就是我正在这样做,这意味着您需要手动编写缩放图像支持,您不能像 UIImage 的
-imageNamed:
对于本地文件一样免费获得它。I use some code like this for the same purpose:
Only iOS 4.x+ devices support the UIScreen
scale
instance method. And since iPhone4's don't run iOS 3, we can rule those out right away. Next, we check if the scale factor is 2.0, if so we know it has a retina display.While this isn't definitive (apple could release another retina device tomorrow), it does test 'model' where it's important -- i.e., you could be fetching images from a web service that provides @2x images and standard images, which is what I'm doing, which means you need to write the scaling image support manually, you don't get it for free as with UIImage's
-imageNamed:
for local files.您可以通过 UIDevice 类获取确切的模型:
此处记录了此方法和其他一些方法: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html
但要小心,不要混淆硬件型号和 iOS 版本。如果您想为支持它们的设备提供额外/不同的功能,最好使用 respondsToSelector: 方法或 NSClassFromString 函数检查该特定接口是否可用。
不过,使用 NSClassFromString 函数时要小心,因为某些类作为早期 SDK 中私有 API 的一部分存在,具有完全不同的接口。
You can get the exact model through the UIDevice class:
This, and some other methods are documented here: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html
Be careful, though, to not confuse hardware model with iOS version. If you want to provide extra/different functionality for devices that support them, it's better to check if that specific interface is available, using the respondsToSelector: method, or the NSClassFromString function.
Be careful with using the NSClassFromString function, though, since some classes exists as a part of the private API in the earlier SDK:s, with a completely different interface.