13.8.11

iOS SDK Snippet: iPhone Vibration

iOS SDK Snippet: iPhone Vibration

iOS SDK Snippet: iPhone Vibration

iOS SDK Snippet: iPhone Vibration

  • Technology: iOS SDK
  • Difficulty: Beginner
  • Completion Time: 15 Minutes
With this iOS SDK code snippet, you will learn how to cause the iPhone to vibrate with the AudioToolbox framework and a single line of code. You will also learn additional details about iPhone vibration not explained in the code snippet itself, such as how to generate multiple vibrations at once.

The source code referenced in this screencast was written by KevinBomberry and posted to Snipplr, an Envato site dedicated to saving and sharing code.

GO TO LINK TO VIEW VIDEO: iOS SDK Snippet: iPhone Vibration

iPhone OS - Vibration - Simple Version

  1. // NOTE: You need to import the AudioToolbox for access 
    to the vibrate
  2. #import
  3. // The one-liner:
  4. AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
  5. // The function:
  6. - (void)vibrate {
  7. AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
  8. }
  9. // The call from within another method in the same class:
  10. - (void)myMethod {
  11. [self vibrate];
  12. }
View this snippet on Snipplr

iPhone Vibration Notes

  • Vibration will not work on the iPodTouch or the iPad.
  • Calling this function on unsupported devices will do nothing and will not generate a run-time error.
  • The duration of each vibration is fixed to approximately 2 seconds in length and cannot be altered.