5

我有一些设置可以为某些操作启用/禁用振动,但如果设备没有振动能力,我发现显示它们毫无意义。有没有办法检查这个人是否在使用 iPod touch 以及它是否有振动?

4

2 回答 2

5

我不确定除了进行模型检查之外还有其他方法可以做到这一点,这可能不是一个好方法。我知道苹果提供:

 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

如果设备可以振动,它就会振动。在没有振动的设备上,它什么也不做。还有一个电话:

AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

如果它散列功能或设备会发出哔哔声,这将振动设备。

最好只进行设置并对设置进行一些解释,因为用户可能希望在没有振动设备时发出哔声。也许将设置称为“振动警报开/关”以外的其他设置。

于 2011-08-14T03:01:46.187 回答
3

此代码应该做到这一点 - 请注意,它“假定”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。

于 2011-08-14T15:59:48.163 回答