Making the iPhone vibrate
Swift 2.0+
AudioToolbox now presents the kSystemSoundID_Vibrate
as a SystemSoundID
type, so the code is:
import AudioToolbox.AudioServices
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
Instead of having to go thru the extra cast step
(Props to @Dov)
Original Answer (Swift 1.x)
And, here's how you do it on Swift (in case you ran into the same trouble as I did)
Link against AudioToolbox.framework
(Go to your project, select your target, build phases, Link Binary with Libraries, add the library there)
Once that is completed:
import AudioToolbox.AudioServices
// Use either of these
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
The cheesy thing is that SystemSoundID
is basically a typealias
(fancy swift typedef
) for a UInt32
, and the kSystemSoundID_Vibrate
is a regular Int
. The compiler gives you an error for trying to cast from Int
to UInt32
, but the error reads as "Cannot convert to SystemSoundID", which is confusing. Why didn't apple just make it a Swift enum is beyond me.
@aponomarenko's goes into the details, my answer is just for the Swifters out there.
From "iPhone Tutorial: Better way to check capabilities of iOS devices":
There are two seemingly similar functions that take a parameter kSystemSoundID_Vibrate
:
1) AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
2) AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Both of 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. So if you are going to vibrate the device continuously, as an alert, common sense says, use function 2.
First, add the AudioToolbox framework AudioToolbox.framework
to your target in Build Phases.
Then, import this header file:
#import <AudioToolbox/AudioServices.h>