How to detect the colour of an iPhone 5c? [duplica

2020-02-02 04:28发布

问题:

After iPhone 5c announcement, i'm curious if anybody knows an API how to get an iPhone 5c colour? I'm sure everyone will find it convenient loading a corresponding UI colour scheme to the device colour.

I'm thinking about wrapping it in something like the UIDevice category, which will return a UIColor.

Update: @ColinE and @Ortwin Gentz has indicated the availability of private UIDevice instance method calls for it.

Please note, that in case of iPhone 5c, what you are really looking for is deviceEnclosureColor, as deviceColor will always return #3b3b3c, as it is a front colour.

method signature:

-(id)_deviceInfoForKey:(struct __CFString { }*)arg1

UIDevice category for it:

@interface UIDevice (deviceColour)

- (id)_deviceInfoForKey:(struct __CFString { }*)arg1;
- (NSString*)deviceColourString_UntilAppleMakesItPublic;
- (NSString*)deviceEnclosureColour_UntilAppleMakesItPublic;


@end

@implementation UIDevice (deviceColour)

- (NSString*)deviceColourString_UntilAppleMakesItPublic
{
    return [self _deviceInfoForKey:@"DeviceColor"];
}

- (NSString*)deviceEnclosureColour_UntilAppleMakesItPublic
{
    return [self _deviceInfoForKey:@"DeviceEnclosureColor"];
}


@end

回答1:

There's a private API to retrieve both the DeviceColor and the DeviceEnclosureColor. In case of the iPhone 5c, the interesting part is the enclosure color (the device color = front color is always #3b3b3c).

UIDevice *device = [UIDevice currentDevice];
SEL selector = NSSelectorFromString(@"deviceInfoForKey:");
if (![device respondsToSelector:selector]) {
    selector = NSSelectorFromString(@"_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/

Warning: As mentioned, this is a private API. Don't use this in App Store builds.



回答2:

The device colour (used to?) be encoded in the serial number of the device. I don't have a device to test on with them not officially released yet, but I imagine the solution will be similar to this:

Typical format of the iPhone SN is as follows: AABCCDDDEEF

AA = Factory and Machine ID
B = Year of Manufacturing (9 is 2009/2019, 0 is 2010/2020, 1 is 2011 and so on)
CC = Production Week (01 is week 1 of B, 11 is week 11 of B and so on)
DDD = Unique Identifier
EE = Color (A4=black)
F = size (S=16GB, T=32GB)

[Source]

There is more information on old techniques here


I would like to point out however, that I expect there isn't a supported method of getting the serial number. Assuming you'd only like to know this information so that you can customise your UI, I'd just put in a user option or ask them to select the colour of their device on first startup (or some other early point in the app-user life)



回答3:

A number of people on Twitter have cited the following method:

[[UIDevice currentDevice] _deviceInfoForKey:@"DeviceColor"]

Although I have not confirmed it myself.



回答4:

This may help you...

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"]);
    }

Ref: Detecting Color of iPhone/iPad/iPod touch?