Iphone 我能否确定/如何确定设备是否存在振动?
我有一些设置可以启用/禁用某些动作的振动,但如果设备没有振动能力,则显示这些设置是毫无意义的。有没有办法检查用户是否正在使用iPod touch以及它是否有振动?除了进行型号检查之外,我不确定是否还有其他方法可以进行检查,这可能不是一个很好的方法。我知道苹果公司提供:Iphone 我能否确定/如何确定设备是否存在振动?,iphone,ios,ios4,hardware,vibration,Iphone,Ios,Ios4,Hardware,Vibration,我有一些设置可以启用/禁用某些动作的振动,但如果设备没有振动能力,则显示这些设置是毫无意义的。有没有办法检查用户是否正在使用iPod touch以及它是否有振动?除了进行型号检查之外,我不确定是否还有其他方法可以进行检查,这可能不是一个很好的方法。我知道苹果公司提供: AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 如果设备可以振动,它就会振动。在没有振动的设备上,它不会起任何作用。还有一个电话: AudioServicesPlay
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
如果设备可以振动,它就会振动。在没有振动的设备上,它不会起任何作用。还有一个电话:
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
这一个将振动设备,如果它散列的能力或设备将嘟嘟声
最好只做设置,并对设置进行解释,因为用户在没有振动设备的情况下可能希望听到嘟嘟声。可能会调用除“振动警报开/关”以外的其他设置。此代码应该可以做到这一点-请注意,它“假设”iPhone是唯一具有振动功能的设备。就目前而言
- (NSString *)machine
{
static NSString *machine = nil;
// we keep name around (its like 10 bytes....) forever to stop lots of little mallocs;
if(machine == nil)
{
char * name = nil;
size_t size;
// Set 'oldp' parameter to NULL to get the size of the data
// returned so we can allocate appropriate amount of space
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
// Allocate the space to store name
name = malloc(size);
// Get the platform name
sysctlbyname("hw.machine", name, &size, NULL, 0);
// Place name into a string
machine = [[NSString stringWithUTF8String:name] retain];
// Done with this
free(name);
}
return machine;
}
-(BOOL)hasVibration
{
NSString * machine = [self machine];
if([[machine uppercaseString] rangeOfString:@"IPHONE"].location != NSNotFound)
{
return YES;
}
return NO;
}
只是编辑以阻止机器每次调用时执行大量小malloc。这很好,但有两个问题,我不太理解您使用的C函数-上次使用“name”后不应该调用“free(name)”吗?另一方面,我是否可以更轻松地缓存“hasVibration”的“BOOL”结果而不是字符串?似乎更直接。是的-谢谢你注意到免费的是在它使用的地方之后-我的坏:(-此外,如果需要,你应该缓存结果,我粘贴的函数是我拥有的一个更大类的一部分,它返回各种硬件属性。哦,太好了,然后我会相应地调整。