user757812
user757812

Reputation: 581

how can I check if the iOS device can vibrate

Currently only iPhone supports the vibrations how can I check if my device supports vibrations before calling the vibration function.

Upvotes: 11

Views: 9773

Answers (3)

Stavash
Stavash

Reputation: 14304

Unfortunately there is no documented method for testing whether or not the device supports vibration. From documentation:

On some iOS devices, you can pass the kSystemSoundID_Vibrate constant to invoke vibration. On other iOS devices, calling this function with that constant does nothing.

Looks like the correct approach here would be to simply call the methods mentioned by Saurabh without checking if vibration is supported.

Upvotes: 1

Michael
Michael

Reputation: 3871

A bit of a work-around but I've found this to work. It's based on the assumption that only iPhone devices currently have the vibrate hardware in them.

if([[UIDevice currentDevice].model isEqualToString:@"iPhone"])
{
    // An iPhone: so should have vibrate
}
else
{
    // Not an iPhone: so doesn't have vibrate
}

Upvotes: 4

Saurabh
Saurabh

Reputation: 22873

The iOS sdk has two functions that would vibrate the iPhone. But Vibration hardware is present only on iPhones. So how will you alert your user who uses the app on iPad or iPod touches? Clearly, checking the model is not the way to go. There are two seemingly similar functions that take a parameter kSystemSoundID_Vibrate

AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

Both the functions vibrate the iPhone. But when you use the first function on devices that don’t support vibration, it plays a beep sound. The second function on the other hand does nothing on unsupported devices.

Upvotes: 10

Related Questions