Skip to content

Instantly share code, notes, and snippets.

@username0x0a
Last active November 22, 2020 18:13
Show Gist options
  • Save username0x0a/a8ef32fd34e6b66f071ef3c546e2559a to your computer and use it in GitHub Desktop.
Save username0x0a/a8ef32fd34e6b66f071ef3c546e2559a to your computer and use it in GitHub Desktop.
UIDevice extension to identify the front face colour of your device
typedef NS_ENUM(NSUInteger, UIDeviceFaceColor) {
UIDeviceFaceColorUnknown = 0,
UIDeviceFaceColorBlack,
UIDeviceFaceColorWhite,
};
...
@implementation UIDevice (Utils)
- (UIDeviceFaceColor)faceColor
{
static UIDeviceFaceColor color = UIDeviceFaceColorUnknown;
#if (TARGET_OS_IOS == 1)
#if !(TARGET_OS_SIMULATOR)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *colStr = nil;
SEL sel = NSSelectorFromString([@[ @"_", @"device", @"Info", @"ForKey:" ] componentsJoinedByString:@""]);
if ([self respondsToSelector:sel])
colStr = [self performSelector:sel withObject:@"DeviceColor"];
if (![colStr isKindOfClass:[NSString class]]) {}
else if ([colStr containsSubstring:@"#3b3b"] ||
[colStr containsSubstring:@"#2727"] ||
[colStr isEqualToString:@"black"])
color = UIDeviceFaceColorBlack;
else if ([colStr containsSubstring:@"#e4e7"] ||
[colStr containsSubstring:@"#e1e4"] ||
[colStr isEqualToString:@"white"])
color = UIDeviceFaceColorWhite;
});
#pragma clang diagnostic pop
#endif
#endif
return color;
}
@end
@username0x0a
Copy link
Author

username0x0a commented Aug 24, 2016

This UIDevice extension allows you to identify the front face colour of your device. It makes use of an undocumented / private API call _deviceInfoForKey: thus it's not considered to be completely safe, but this approach could give you a change for use, probably even in the App Store release. 🎱 Should work well with iOS7+. For iOS6, perform the call on deviceInfoForKey: without the leading _.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment