Search code examples
iphoneiosios4hardwarevibration

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


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?


Solution

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