如果设备有振动,我可以确定/如何确定? [英] Can I determine / how, if a device has vibration or not?

查看:142
本文介绍了如果设备有振动,我可以确定/如何确定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some settings that enable/disable vibration for certain actions, but I find it pointless to display them if the device doesn't have the ability to vibrate. Is there a way to check if the person is using an iPod touch and if it has vibration?

推荐答案

此代码应该这样做 - 请注意,它假定iPhone是唯一具有振动功能的设备。它暂时是...

This code should do it - be aware it 'assumes' the iPhone is the only device with Vibration capability. Which it is for the moment...

- (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叫。

Just edited to stop the machine call from doing lots of small mallocs each time its called.

这篇关于如果设备有振动,我可以确定/如何确定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆