Determining if device can vibrate - iPhone vs iPod Touch
Developing an app that vibrates when a particular event occurs. I have a setting for turning the vibrate option ON or OFF.
I would like to be able to disable the display of the vibrate setting for devices like the iPod Touch that do not have vibrate capability. I know I can do this by determining the device model using:
UIDevice *thisDevice = [UIDevice currentDevice];
modelOfDevice = [thisDevice model];
I can then disable the Vibrate option depending on the modelOfDevice.. (i.e. not display it for iPod Touch). This works - but, I think it's bad for开发者_JS百科m.. for example, if future iPod Touch devices do include Vibrate functionality, this solution would break.
So, the question.. How do I check to see if a device has the capability to vibrate??
Any suggestions appreciated. Thanks in advance.
If you just use AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) it vibrates where it can and does nothing where it cannot - just as the Apple documentation says, so you could skip the detection. The [article][1] says just can do AudioServicesPlayAlertSound(kSystemSoundID_Vibrate) to vibrate where possible or beep where not (iPod Touch still have beeping/keypress-click-sound ability)
[1]: http://blog.mugunthkumar.com/coding/iphone-tutorial-better-way-to-check-capabilities-of-ios-devices/ article
I don't know if it's possible to check if a device is vibration capable.
But it's possible to identify device type.
- (NSString *)platformRawString
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithUTF8String:machine];
free(machine);
return platform;
}
- (NSString *)platformNiceString {
NSString *platform = [self platformRawString];
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([platform isEqualToString:@"iPhone3,3"]) return @"Verizon iPhone 4";
if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone6 Plus";
if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
if ([platform isEqualToString:@"iPad1,1"]) return @"iPad 1";
if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3 (WiFi)";
if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3 (4G,2)";
if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3 (4G,3)";
if ([platform isEqualToString:@"i386"]) return @"Simulator";
if ([platform isEqualToString:@"x86_64"]) return @"Simulator";
return platform;
}
Call the platformNiceString method .
NSString *device = [self platformNiceString];
if ([device isEqualToString: @"iPhone6 Plus"]) //do rest of the code here
But you'll have to identify each device manually.
精彩评论