Vibrate iPhone or Handy Debugging Trick

I’ve covered playing sounds three times in the past: AVAudioPlayer, Audio Session Services and Silent Mode Switch and Playing Sounds.

Even with all the above options, I recently was in need of a simple way (read, with little code) to have an audible cue to know when my app reached a certain point of execution. The reason being, the section of code I needed to debug was not accessible through a breakpoint, as the code was called as part of a Push Notification and was only invoked if the app was not running.

I began my search hoping to find a simple way to play a beep (as is common on non-smart phone devices or a PC). What I settled on was calling a method to vibrate the phone, which in turn also has an audible effect. The code below is handy whenever you need to vibrate the iPhone, for whatever reason.

Vibrating the iPhone

Here are the steps:

  1. Add the AudioToolbox framework to your app
  2. Import the AudioToolbox header file
  3. Call the method void AudioServicesPlayAlertSound (SystemSoundID inSystemSoundID)

In place of creating your own SystemSoundID from a file or other resource (see Audio Session Services), you can use the constant kSystemSoundID_Vibrate.

#import <AudioToolbox/AudioToolbox.h>
 
...
 
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
And for the iPod touch?

Unfortunately, if you run this code on an iPod touch, it has no effect. Note to self, debug the application on an iPhone before running on an iPod.

  1. Hi John,

    Vibrating the phone is very cool, but I’ve used a couple other techniques in the past to help debug push notifications.

    The first way is to just simulate the push notification payload in application:didFinishLaunchingWithOptions: This works well at the beginning, allowing you to make sure all of your code paths are being executed properly without having to set up a push server. I just use a static dictionary instead of the one provided by the launchOptions.

    The second way is to tell xcode to start debugging as soon as the application is launched. You start the debugger, but the app doesn’t run until you launch it manually, or you open from a push notification.

    To do this in XCode 4, click Product > Edit Scheme, then in the Run phase switch the Launch setting to be “Wait for ….app to launch”

    I hope this helps!

  2. did you know you can tell xcode to start the debugger the next time the application is executed?
    this is exactly what you need in order to debug a push when the app is closed..

    double click on the executable, go to the debugging tab, and select the checkbox “Wait for next launch/push notification” it will disable the “Start executable after starting debugger”

    when you’ll start the debug, it will just install the app on the device, and then stop.
    then when the push arrives and you select to open the app, xcode will attach to that process, so if you have breakpoints – it will break…

    hope it helps,
    Yaniv

  3. Cool. Good Stuff! I didn’t know about the “Wait for next launch/push notification option.

Comments are closed.