Making the iPhone vibrate
Solution 1
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>
Solution 2
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.
Solution 3
A simple way to do so is with Audio Services:
#import <AudioToolbox/AudioToolbox.h>
...
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Solution 4
I had great trouble with this for devices that had vibration turned off in some manner, but we needed it to work regardless, because it is critical to our application functioning, and since it is just an integer to a documented method call, it will pass validation. So I have tried some sounds that were outside of the well documented ones here: TUNER88/iOSSystemSoundsLibrary
I have then stumbled upon 1352, which is working regardless of the silent switch or the settings on the device (Settings->vibrate on ring, vibrate on silent)
.
- (void)vibratePhone;
{
if([[UIDevice currentDevice].model isEqualToString:@"iPhone"])
{
AudioServicesPlaySystemSound (1352); //works ALWAYS as of this post
}
else
{
// Not an iPhone, so doesn't have vibrate
// play the less annoying tick noise or one of your own
AudioServicesPlayAlertSound (1105);
}
}
Solution 5
Important Note: Alert of Future Deprecation.
As of iOS 9.0, the API functions description for:
AudioServicesPlaySystemSound(inSystemSoundID: SystemSoundID)
AudioServicesPlayAlertSound(inSystemSoundID: SystemSoundID)
includes the following note:
This function will be deprecated in a future release.
Use AudioServicesPlayAlertSoundWithCompletion or
AudioServicesPlaySystemSoundWithCompletion instead.
The right way to go will be using any of these two:
AudioServicesPlayAlertSoundWithCompletion(kSystemSoundID_Vibrate, nil)
or
AudioServicesPlayAlertSoundWithCompletion(kSystemSoundID_Vibrate) {
//your callback code when the vibration is done (it may not vibrate in iPod, but this callback will be always called)
}
remember to
import AVFoundation
Related videos on Youtube
some_id
Updated on May 15, 2020Comments
-
some_id almost 4 years
How can the iPhone be set to vibrate once?
For example, when a player loses a life or the game is over, the iPhone should vibrate.
-
Eiko over 13 yearsShake gesture is completely different than vibration. One is human-initiated, one device-initiated.
-
-
some_id over 13 yearsIs this the only way to vibrate the phone? Why did they run it through AudioServices?
-
Raptor over 12 years#import <Cocoa/Cocoa.h> is not required.
-
George Asda over 11 yearsIs there a way to reduce the vibration time to less than 1 sec?
-
Denis Kutlubaev about 11 yearsI would like to add, that if vibration is off in Settings of iOS, user will not get vibration even if you use these commands.
-
Jonny about 11 yearsAny info on general stance from Apple on vibration? Any case where they turned an app down because of improper vibration use?
-
Michael Mangold over 10 years#import <AudioToolbox/AudioToolbox.h> and you'll need to add the AudioToolbox.framework to your project's Build Phases.
-
Sam Soffes almost 10 yearsIn Swift:
AudioServicesPlayAlertSound(UInt32(kSystemSoundID_Vibrate))
(at least as of beta 2) -
Mangesh almost 10 yearsAudioServicesPlayAlertSound(kSystemSoundID_Vibrate); Vibarate on iPhone proerly but it doesn't make any beep sound on iPad.
-
nalexn over 9 yearsIt's always better to use named aliases instead of magic constants, like kSystemSoundID_Vibrate instead 1352. I encourage you to update your answer.
-
Grimxn about 9 yearsLooks like he's not going to update it - why not edit the answer yourself?
-
marcshilling almost 9 yearsI would agree using this magic 1352 is not ideal, but I can't find any other way to force a vibration even when the vibrate switch is off on the device. This seems to be the only way.
-
medvedNick almost 9 years@JoelTeply I confirm,
kSystemSoundID_Vibrate
not worked for me, but1352
did. Where did you find this constant? -
Joel Teply almost 9 yearsI wrote a for loop with integers starting past the published constants and figured out which one caused a vibrate
-
vomako almost 9 yearsI can confirm that the iPhone vibrates even if the silent mode is activated on the iPhone. Great answer!
-
Joel Teply almost 9 yearsWhat I mean to say is that I hacked the Gibson
-
DDPWNAGE almost 9 yearsiPod touches and iPads can't vibrate.
-
Vatsal Shukla over 8 yearsthis works great when your app is in foreground state but what when in background state like, bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ function 1 or function 2 for vibration }]; this is not working. can you explain?
-
Dov over 8 yearsThis seems to be fixed in Swift 2/iOS 9/Xcode 7. I used
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
and it compiled fine -
Juan Boero over 8 yearsyou are missing the import statement
-
Hugo Alonso over 8 yearsimport AudioToolbox.AudioServices
-
Juan Boero over 8 yearsi did it myself, is just with the AVFoundation one.
-
JustAnotherCoder over 8 yearsAudioServicesPlaySystemSound (1352) still works for iPhones regardless of silent switch position as of Jan 2016
-
Hugo Alonso about 8 yearsThis functions are going to be deprecated in a future release of iOS.
-
Micro almost 8 yearsAnyway to set duration of vibration?
-
Hugo Alonso almost 8 yearsYou are going to need to concatenate several calls, use a time lapse in between every new call
-
Jano almost 7 yearsAudioServicesPlaySystemSound (1352) works with target 9.3 in iOS 11 beta 2 even with vibration disabled + night mode.
-
Lal Krishna over 6 yearson a device with no vibration capability (like iPod Touch) this will do nothing
-
FedeH over 5 yearsKeep in mind that the device does not vibrate if your app’s audio session is configured with the AVAudioSessionCategoryPlayAndRecord or AVAudioSessionCategoryRecord audio session category. This ensures that vibration doesn’t interfere with audio recording.
-
Bhavesh.iosDev over 5 yearshow to increase/Decrease Vibration time.
-
Benj almost 5 yearsJust remember to
import UIKit
! -
rmaddy almost 5 yearsYou don't want to create a new instance of
haptic
inside this method. You are not callingimpactOccured
on the same instance that you callprepare
. -
Simon Pickup about 4 yearsThe deprecation warning no longer exists.
-
Diken Shah over 3 yearsHello @FedeHenze How can we achieve vibration with the recording? Is there another way to achieve vibration wile recording?
-
Diken Shah over 3 yearsHello @Benj I want to implement vibration with audio recording and haptic does not work while recording.
-
Div over 3 years@DikenShah as stated, I believe I read somewhere this is a deliberate move to minimise microphone interference. As microphones detect vibrations, and the Taptic Engine makes vibrations, this would clearly be an issue.
-
Diken Shah over 3 years@Div I found the solution. Thanks for your answer