Determine device (iPhone, iPod Touch) with iPhone

2018-12-31 01:24发布

Is there a way to determine the device running an application. I want to distinguish between iPhone and iPod Touch, if possible.

30条回答
零度萤火
2楼-- · 2018-12-31 02:17

You can check GBDeviceInfo on GitHub, also available via CocoaPods. It provides simple API for detecting various properties with support of all latest devices:

  • Device family

[GBDeviceInfo deviceDetails].family == GBDeviceFamilyiPhone;

  • Device model

[GBDeviceInfo deviceDetails].model == GBDeviceModeliPhone6.

For more see Readme.

查看更多
倾城一夜雪
3楼-- · 2018-12-31 02:18

I took it a bit further and converted the big "isEqualToString" block into a classification of bit masks for the device type, the generation, and that other qualifier after the comma (I'm calling it the sub generation). It is wrapped in a class with a singleton call SGPlatform which avoids a lot of repetitive string operations. Code is available https://github.com/danloughney/spookyGroup

The class lets you do things like this:

if ([SGPlatform iPad] && [SGPlatform generation] > 3) {
    // set for high performance
}

and

switch ([SGPlatform deviceMask]) {
case DEVICE_IPHONE:
    break;
case DEVICE_IPAD:
    break;
case DEVICE_IPAD_MINI:
    break;
}

The classification of the devices is in the platformBits method. That method should be very familiar to the readers of this thread. I have classified the devices by their type and generation because I'm mostly interested in the overall performance, but the source can be tweaked to provide any classification that you are interested in, retina screen, networking capabilities, etc..

查看更多
千与千寻千般痛.
4楼-- · 2018-12-31 02:19

Please feel free to use this class (gist @ github)

CODE REMOVED AND RELOCATED TO

https://gist.github.com/1323251

UPDATE (01/14/11)

Obviously, this code is a bit out of date by now, but it can certainly be updated using the code on this thread provided by Brian Robbins which includes similar code with updated models. Thanks for the support on this thread.

查看更多
低头抚发
5楼-- · 2018-12-31 02:20

There's a pretty mature library (by me) for that called SDVersion. You can check for running device's model, screen size and many other parameters. It also supports OSX.

Example:

      // Check for device model
      if ([SDVersion deviceVersion] == iPhone6)
           NSLog(@"You got the iPhone 6. Sweet                                                                     
查看更多
公子世无双
6楼-- · 2018-12-31 02:23

For simple comparison I always like macro:

#define IS_IPOD [[UIDevice currentDevice].model containsString:@"iPod"]
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

It's simple and easy to use.

查看更多
笑指拈花
7楼-- · 2018-12-31 02:25

I'd like to add that to retrieve the front and enclosure color of the device there's a private API:

UIDevice *device = [UIDevice currentDevice];
SEL selector = NSSelectorFromString([device.systemVersion hasPrefix:@"7"] ? @"_deviceInfoForKey:" :  @"deviceInfoForKey:");
if ([device respondsToSelector:selector]) {
    NSLog(@"DeviceColor: %@ DeviceEnclosureColor: %@", [device performSelector:selector withObject:@"DeviceColor"], [device performSelector:selector withObject:@"DeviceEnclosureColor"]);
}

I've blogged about this and provide a sample app:

http://www.futuretap.com/blog/device-colors/

查看更多
登录 后发表回答