Last active
November 22, 2020 18:13
-
-
Save username0x0a/a8ef32fd34e6b66f071ef3c546e2559a to your computer and use it in GitHub Desktop.
UIDevice extension to identify the front face colour of your device
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ondeviceInfoForKey:
without the leading_
.