开发者

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

开发者 https://www.devze.com 2023-03-27 19:00 出处:网络
I have some settin开发者_JAVA技巧gs 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 che

I have some settin开发者_JAVA技巧gs 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?


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".


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号