Can I determine / how, if a device has vibration or not?

狂风中的少年 提交于 2019-12-05 08:38:24

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;
}

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

I'm not sure there is a way to do this other than doing model checks which is probably not a great approach. I do know that apple provides:

 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

If the device can vibrate, it will. On devices without vibration, it will do nothing. There is another call:

AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

This one will vibrate the device if it hash the capability or the device will beep.

It might be better to just have the settings and have some explanation around the setting because a user may want the beep when they do not have a vibrating device. Maybe call the setting something other than "Vibration Alert On/Off".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!