Skip to content

Instantly share code, notes, and snippets.

@izackp
Last active August 29, 2015 14:05
Show Gist options
  • Save izackp/2ee0ca4b6c731b254e55 to your computer and use it in GitHub Desktop.
Save izackp/2ee0ca4b6c731b254e55 to your computer and use it in GitHub Desktop.
A more accurate check for retina in iOS. (Assumes all future devices are retina)
@interface NSString (Formatting)
- (NSString*)stringWithOnlyLetters;
- (NSString*)stringWithOnlyNumbers;
@end
#import "NSString+Formatting.h"
@implementation NSString (Formatting)
- (NSString*)stringWithOnlyLetters {
NSCharacterSet *numSet = [[NSCharacterSet letterCharacterSet] invertedSet];
return [[self componentsSeparatedByCharactersInSet:numSet] componentsJoinedByString:@""];
}
- (NSString*)stringWithOnlyNumbers {
NSCharacterSet* letters = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
return [[self componentsSeparatedByCharactersInSet:letters] componentsJoinedByString:@""];
}
@end
/*! @attention Not accurate on the simulator, and will not be accurate on future devices that use retina resources but actually have a non retina display*/
extern bool hasRetinaDisplay(void);
extern bool usesRetinaResources(void);
#import "RetinaDisplayMethods.h"
#import <NSString+Formatting.h>
#import <sys/utsname.h>
bool hasRetinaDisplay() {
struct utsname systemInfo;
uname(&systemInfo);
NSString* code = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
NSString* deviceType = [code stringWithOnlyLetters];
NSString* deviceVersion = [code stringWithOnlyNumbers];
int version = [deviceVersion intValue];
if ([deviceType isEqualToString:@"iPad"])
{
if (version < 30)
return false;
}
if ([deviceType isEqualToString:@"iPod"])
{
if (version < 50)
return false;
}
if ([deviceType isEqualToString:@"iPhone"])
{
if (version < 40)
return false;
}
return usesRetinaResources();
}
bool usesRetinaResources() {
return ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
([UIScreen mainScreen].scale == 2.0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment