Servin Mobile Software


How To Program Your iPhone To Vibrate and Display an Alert


Norman McEntire

Version 1.1 January 21

Copyright © 2009 Servin Corporation. http://servin.com


Introduction

In this Servin Mini-Course, you will write an iPhone program in Objective-C 2.0 that vibrates and displays an alert when you touch the screen.

NOTE: Although the iPhone and iPod Touch are similar, this is one area where the two hardware platforms differ: the iPod Touch does not vibrate. You can execute this code on either platform, but only the iPhone will vibrate.


iPhone Programming Skills You Will Learn


Prerequisites


Startup Xcode

If Xcode is not already running, start it up:

  1. On the Mac Desktop, double-click on the Finder icon (the icon with the face on it).
  2. On the left-side of the Finder window, click on Macintosh HD.
  3. On the right-side of the Finder window, click on the Developer folder.
  4. Click on the Applications folder.
  5. Double-click on the Xcode icon.

At this point, you should see the Xcode menu at the top of your desktop.


Create New Xcode Project

With Xcode running, create a new Xcode project:

  1. File > New Project.
  2. On the left-side of the New Project window, select Application under iPhone OS.
  3. On the right-side of the New Project window, select View-Based Application.
  4. Click on the Choose button.
  5. Enter Vibrate for the project name.
  6. Click on Save button.

At this point, you should see Xcode open a new window that shows a number of files.


Build Default App

Go ahead and build the default application:

  1. Click on the Build and Go button.
  2. After a brief moment, you should see the code running in the iPhone Simulator.
  3. Observe the status bar at the top of the window, and a gray window everywhere else.
  4. In the iPhone Simulator, press the Home button to stop the application.

Use Interface Builder to add Navigation Bar

In this exercise, you will use Interface Builder to add a Navigation Bar to your view.

  1. In Xcode, in the Groups & Files window on the left-side, click on the Resources folder.
  2. You should see the following files on the right-side of the window:
    - VibrateViewController.xib
    - Info.plist
    - MainWindow.xib
    
  3. Double-click on VibrateViewController.xib.
  4. After a brief moment, you should see Interface Builder start up.
  5. Observe the window titled View with a gray background.
  6. Use the Interface Builder menu to select Tools > Library.
  7. In the Library window, expand the word Library to match the following:
    - Library
    	- Cocoa Touch Plugin
    		- Controllers
    		- Data Views
    		- Inputs & Values;
    		- Windows, Views, & Bars
    
  8. Select Windows, Views, & Bars.
  9. Drag Navigation Bar from the Library window to your gray View window, positioning it near the top of the window, below the status bar.
  10. Use the Interface Builder menu to select Tools > Size Inspector.
  11. Using the Size Inspector, make sure the X/Y values are set to 0, i.e., ensure that the Navigation Bar is at the top of the screen:
    X: 0
    Y: 0
    
  12. Use the Interface Builder menu to select Tools > Attributes Inspector.
  13. In the window titled Navigation Item Attributes, set the title to "Touch Screen To Vibrate".
  14. Use the Interface Builder menu to select File > Save.
  15. Press Command+Tab and return to Xcode.
  16. Click on Build and Go.
  17. Observe that your application now has the Navigation Bar with the text "Touch Screen To Vibrate". Show screen shot..
  18. Press Home on the iPhone Simulator to exit the application.

As a review, in this exercise you used Interface Builder to place a Navigation Bar on your view, and you set the title to "Touch Screen To Vibrate".


Edit VibrateViewController To Vibrate on Touch

In this exercise, you will edit VibrateViewController.m, adding a touchesBegan:withEvent: function to issue a vibration.

  1. In Xcode, in the Groups & Files window on the left-side, click on the Classes folder.
  2. You should see the following files on the right-side of the window:
    VibrateAppDelegate.h
    VibrateAppDelegate.m
    VibrateViewController.h
    VibrateViewController.m
    
  3. Select VibrateViewController.h into the Xcode editor window.
  4. Edit the code so that it matches the following:
    #import <UIKit/UIKit.h>
    
    #import <AudioToolbox/AudioToolbox.h>
    
    @interface VibrateViewController : UIViewController {
    
    }
    @end
    
  5. Select VibrateViewController.m into the Xcode editor window.
  6. Add the following method to the existing code:
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    	// Issue vibrate
    	AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    
    	// Also issue visual alert
    	UIAlertView *alert = [[UIAlertView alloc]
    			      initWithTitle:@"Screen Touched!"
    			      message:nil
    			      delegate:nil
    			      cancelButtonTitle:nil
    			      otherButtonTitles:@"OK", nil];
    	[alert show];
    }
    
    
  7. Build and Go, but you will see an error.
  8. At the lower-right of your Xcode screen, you should see a red "x".
  9. Click on the red "x", and you will see a window titled Build Results.
  10. The error you should see is a linking error:
    Linking...
    	_AudioServicesPlaySystemSound...
    	symbol(s) not found...
    
  11. The error is because the AudioToolbox.framework is not part of your project. You will solve this error in the next exercise.

As a review, in this exercise you added the AudioServicesPlaySystemSound() function to generate a vibration. You also used the UIAlertView to display a visual alert.


Adding AudioToolBox Framework

In this exercise, you will add the AudioToolbox Framework to your project.

  1. In the Xcode Groups & Files window, click on Frameworks.
  2. Observe that there are three Frameworks in the default build environment:
  3. With the Framework folder still selected, control-click to display a popup menu.
  4. On the popup menu, select Add > Existing Framework....
  5. You should see a file selection window displayed, with the name Frameworks as the selected folder.
  6. Click on Frameworks, then click on AudioToolbox.framework.
  7. With AudioToolbox.framework selected, press the Add button.
  8. You will see a drop-down window showing Add To Targets.
  9. Press Add to add the framework to your project.
  10. Confirm that the framework was added by looking for AudioToolbox.framework shown in the Xcode Frameworks folder.
  11. Click on Build and Go and the project should build and run, i.e., you no longer have the link error.
  12. While the application runs, touch the screen and observe the results. Show screen shot.

As a review, in this exercise you added the AudioToolbox.framework to your project to resolve link errors.


Skills Review


How To Contact Author

Feel free to contact the author for any of the following:


Updated 2009 Jan 21
Content viewable on all web browsers, including smart mobile phone devices.
Copyright © 1995-2009 Servin Corporation. All rights reserved.