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
- How to use Xcode to create a View-Based App.
- How to use Interface Builder to add a navigation bar to the layout.
- How to use Interface Builder to set the title of the navigation bar.
- How to execute code when the user touches the screen.
- How to make the iPhone vibrate.
- How to to display an alert.
Prerequisites
Startup Xcode
If Xcode is not already running, start it up:
- On the Mac Desktop, double-click on the Finder icon (the icon with the face on it).
- On the left-side of the Finder window, click on Macintosh HD.
- On the right-side of the Finder window, click on the Developer folder.
- Click on the Applications folder.
- 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:
- File > New Project.
- On the left-side of the New Project window, select Application under iPhone OS.
- On the right-side of the New Project window, select View-Based Application.
- Click on the Choose button.
- Enter Vibrate for the project name.
- 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:
-
Click on the Build and Go button.
-
After a brief moment, you should see the code running in the iPhone Simulator.
-
Observe the status bar at the top of the window,
and a gray window everywhere else.
-
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.
-
In Xcode,
in the Groups & Files window on the left-side,
click on the Resources folder.
-
You should see the following files on the right-side of the window:
- VibrateViewController.xib
- Info.plist
- MainWindow.xib
-
Double-click on VibrateViewController.xib.
-
After a brief moment,
you should see Interface Builder start up.
-
Observe the window titled View with a gray background.
-
Use the Interface Builder menu to select
Tools > Library.
-
In the Library window,
expand the word Library to match the following:
- Library
- Cocoa Touch Plugin
- Controllers
- Data Views
- Inputs & Values;
- Windows, Views, & Bars
-
Select Windows, Views, & Bars.
-
Drag Navigation Bar from the Library window to
your gray View window,
positioning it near the top of the window,
below the status bar.
-
Use the Interface Builder menu to select
Tools > Size Inspector.
-
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
-
Use the Interface Builder menu to select
Tools > Attributes Inspector.
-
In the window titled Navigation Item Attributes,
set the title to "Touch Screen To Vibrate".
-
Use the Interface Builder menu to select
File > Save.
-
Press Command+Tab and return to Xcode.
-
Click on Build and Go.
-
Observe that your application now has the Navigation Bar
with the text "Touch Screen To Vibrate".
Show screen shot..
-
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.
-
In Xcode,
in the Groups & Files window on the left-side,
click on the Classes folder.
-
You should see the following files on the right-side of the window:
VibrateAppDelegate.h
VibrateAppDelegate.m
VibrateViewController.h
VibrateViewController.m
-
Select VibrateViewController.h into the Xcode editor window.
-
Edit the code so that it matches the following:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface VibrateViewController : UIViewController {
}
@end
-
Select VibrateViewController.m into the Xcode editor window.
-
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];
}
-
Build and Go,
but you will see an error.
-
At the lower-right of your Xcode screen,
you should see a red "x".
-
Click on the red "x",
and you will see a window titled Build Results.
-
The error you should see is a linking error:
Linking...
_AudioServicesPlaySystemSound...
symbol(s) not found...
-
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.
-
In the Xcode Groups & Files window,
click on Frameworks.
-
Observe that there are three Frameworks in the default build environment:
- UIKit.framework
- Foundation.framework
- CoreGraphics.framework
-
With the Framework folder still selected,
control-click to display a popup menu.
-
On the popup menu, select Add > Existing Framework....
-
You should see a file selection window displayed, with the name
Frameworks as the selected folder.
-
Click on Frameworks,
then click on AudioToolbox.framework.
-
With AudioToolbox.framework selected,
press the Add button.
-
You will see a drop-down window showing Add To Targets.
-
Press Add to add the framework to your project.
-
Confirm that the framework was added by looking for
AudioToolbox.framework shown in the Xcode Frameworks folder.
-
Click on Build and Go and the project should build
and run, i.e., you no longer have the link error.
-
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
- Xcode
- View-Based Application
- Interface Builder
- Navigation Bar
- Navigation Bar Title
- touchesBegan:withEvent:
- AudioServicesPlaySystemSound()
- kSystemSoundID_Vibrate
- UIAlertView
How To Contact Author
Feel free to contact the author
for any of the following:
- You have a question or comment about this mini-course.
- You need to hire Servin to help with your software development project.
- You need to hire Servin to give an on-site training course for your software development team.
Updated 2009 Jan 21
Content viewable on all web browsers, including smart mobile phone devices.
Copyright © 1995-2009 Servin Corporation. All rights reserved.