Skip to content

Instantly share code, notes, and snippets.

@onevcat
Created September 17, 2013 13:12
Show Gist options
  • Save onevcat/6594133 to your computer and use it in GitHub Desktop.
Save onevcat/6594133 to your computer and use it in GitHub Desktop.
Support AirDrop or not
#import <sys/types.h>
#import <sys/sysctl.h>
+ (NSString *)machineName
{
size_t size;
if (sysctlbyname("hw.machine", NULL, &size, NULL, 0) < 0) {
return nil;
}
char *name = malloc(size);
if (sysctlbyname("hw.machine", name, &size, NULL, 0) < 0) {
free(name);
return nil;
}
NSString *machine = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
free(name);
return machine;
}
+ (BOOL)machineComponentsWithName:(NSString **)theName major:(NSString **)theMajor minor:(NSString **)theMinor
{
NSString *machineName = [self machineName];
if ([machineName length]) {
int ii = 0;
for (; ii < [machineName length]; ii++) {
unichar c = [machineName characterAtIndex:ii];
if (c >= '0' && c <= '9') {
// End of name, begin of major
if (theName) {
*theName = [machineName substringToIndex:ii];
break;
}
}
}
int jj = ii;
for (; jj < [machineName length]; jj++) {
if ([machineName characterAtIndex:jj] == ',') {
if (theMajor) {
*theMajor = [machineName substringWithRange:NSMakeRange(ii, jj - ii)];
}
if (theMinor) {
*theMinor = [machineName substringFromIndex:jj + 1];
}
return YES;
}
}
}
return NO;
}
+ (BOOL)supportsAirdrop
{
if ([RCActivityUtil iOS7]) { // This checks if the device is running iOS 7 or greater, use [[UIDevice currentDevice] systemVersion]
NSString *theName = nil;
NSString *theMajor = nil;
NSString *theMinor = nil;
if ([self machineComponentsWithName:&theName major:&theMajor minor:&theMinor]) {
if ([theName isEqualToString:@"iPhone"] || [theName isEqualToString:@"iPod"]) {
return [theMajor intValue] >= 5;
}
if ([theName isEqualToString:@"iPad"]) {
return ([theMajor intValue] == 2 && [theMinor intValue] >= 5) || // iPad mini
([theMajor intValue] == 3 && [theMinor intValue] >= 4) || // iPad 4
[theMajor intValue] >= 4; // Suppose all future models will support AirDrop
}
}
}
return NO;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment