Making the iPhone vibrate

125,704

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

Share:
125,704

Related videos on Youtube

some_id
Author by

some_id

Updated on May 15, 2020

Comments

  • some_id
    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
      Eiko over 13 years
      Shake gesture is completely different than vibration. One is human-initiated, one device-initiated.
  • some_id
    some_id over 13 years
    Is this the only way to vibrate the phone? Why did they run it through AudioServices?
  • Raptor
    Raptor over 12 years
    #import <Cocoa/Cocoa.h> is not required.
  • George Asda
    George Asda over 11 years
    Is there a way to reduce the vibration time to less than 1 sec?
  • Denis Kutlubaev
    Denis Kutlubaev about 11 years
    I 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
    Jonny about 11 years
    Any info on general stance from Apple on vibration? Any case where they turned an app down because of improper vibration use?
  • Michael Mangold
    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
    Sam Soffes almost 10 years
    In Swift: AudioServicesPlayAlertSound(UInt32(kSystemSoundID_Vibrate)) (at least as of beta 2)
  • Mangesh
    Mangesh almost 10 years
    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); Vibarate on iPhone proerly but it doesn't make any beep sound on iPad.
  • nalexn
    nalexn over 9 years
    It's always better to use named aliases instead of magic constants, like kSystemSoundID_Vibrate instead 1352. I encourage you to update your answer.
  • Grimxn
    Grimxn about 9 years
    Looks like he's not going to update it - why not edit the answer yourself?
  • marcshilling
    marcshilling almost 9 years
    I 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
    medvedNick almost 9 years
    @JoelTeply I confirm, kSystemSoundID_Vibrate not worked for me, but 1352 did. Where did you find this constant?
  • Joel Teply
    Joel Teply almost 9 years
    I wrote a for loop with integers starting past the published constants and figured out which one caused a vibrate
  • vomako
    vomako almost 9 years
    I can confirm that the iPhone vibrates even if the silent mode is activated on the iPhone. Great answer!
  • Joel Teply
    Joel Teply almost 9 years
    What I mean to say is that I hacked the Gibson
  • DDPWNAGE
    DDPWNAGE almost 9 years
    iPod touches and iPads can't vibrate.
  • Vatsal Shukla
    Vatsal Shukla over 8 years
    this 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
    Dov over 8 years
    This seems to be fixed in Swift 2/iOS 9/Xcode 7. I used AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) and it compiled fine
  • Juan Boero
    Juan Boero over 8 years
    you are missing the import statement
  • Hugo Alonso
    Hugo Alonso over 8 years
    import AudioToolbox.AudioServices
  • Juan Boero
    Juan Boero over 8 years
    i did it myself, is just with the AVFoundation one.
  • JustAnotherCoder
    JustAnotherCoder over 8 years
    AudioServicesPlaySystemSound (1352) still works for iPhones regardless of silent switch position as of Jan 2016
  • Hugo Alonso
    Hugo Alonso about 8 years
    This functions are going to be deprecated in a future release of iOS.
  • Micro
    Micro almost 8 years
    Anyway to set duration of vibration?
  • Hugo Alonso
    Hugo Alonso almost 8 years
    You are going to need to concatenate several calls, use a time lapse in between every new call
  • Jano
    Jano almost 7 years
    AudioServicesPlaySystemSound (1352) works with target 9.3 in iOS 11 beta 2 even with vibration disabled + night mode.
  • Lal Krishna
    Lal Krishna over 6 years
    on a device with no vibration capability (like iPod Touch) this will do nothing
  • FedeH
    FedeH over 5 years
    Keep 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
    Bhavesh.iosDev over 5 years
    how to increase/Decrease Vibration time.
  • Benj
    Benj almost 5 years
    Just remember to import UIKit!
  • rmaddy
    rmaddy almost 5 years
    You don't want to create a new instance of haptic inside this method. You are not calling impactOccured on the same instance that you call prepare.
  • Simon Pickup
    Simon Pickup about 4 years
    The deprecation warning no longer exists.
  • Diken Shah
    Diken Shah over 3 years
    Hello @FedeHenze How can we achieve vibration with the recording? Is there another way to achieve vibration wile recording?
  • Diken Shah
    Diken Shah over 3 years
    Hello @Benj I want to implement vibration with audio recording and haptic does not work while recording.
  • Div
    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
    Diken Shah over 3 years
    @Div I found the solution. Thanks for your answer