Tuesday, July 12, 2011

Vibrate the iPhone device


In this post we will learn how to vibrate the iOS device.
Note : This demo app will not work on the iPod touch as it is not capable of vibrating, so make sure that you deploy this demo app in a iPhone device.
Design Phase: For this application I thought when the user will touch the screen the device must vibrate with an alert view being displayed which says that the screen is touched. Here’s a view at the final output.

Step 1: Open Xcode create a windows based application and give a name to your project and then add a UIViewController subclass to your project with the name myViewController, now you will see that there are two files added into your project with the name myViewController.h and myViewController.m.
Step 2: Inorder to vibrate the device you need to add a framework called as the AudioToolBox framework , and if you are using the latest Xcode SDK i.e 4.2 then the steps to add a framework are somewhat different so lets have a look on how to add a framework with the lastst Xocde SDK.
a.     Select the Xcoce project file and then you will see a view somewhat like this



b.     From their select the target file and press the build phases their you will see the link libraries please select it and press the down arrow


c.     Their your will see the set of frameworks that are present into your application also you may see the add button.
d.     Press the add button this will open a popup with a list of frameworks that are present into your applications please select the appropriate framework of your choice and then press the done button, in this case you will select the AudioToolKit framework.


Step 3: Now please select the myViewController.m file now as per our logic here we have to vibrate the device on the touch of the screen so in that case please copy the touchbegn function from the UIResponder class and paste it inside the myViewControlller.m file. Here’s a look at the body of the function
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
   
     // Displaying the alert view
     UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Screen Touched!"
                          message:nil
                          delegate:nil
                          cancelButtonTitle:nil
                          otherButtonTitles:@"OK", nil];
     [alert show];
    [alert release];
   
}

Code Explanation: There is a function present in the audioToolkit which vibrates the device for you and for that you must supply in the parameter as given above and that’s all you will require. In the next line I am just displaying the alert view which says screen touched that’s all.
Step 4: Now its time to add the view to the iPhone window so for doing that please select the appDelegate.m file and add the following code to present the view into the iPhone window
#import "VibrateDeviceAppDelegate.h"
#import "myViewController.h"
@implementation VibrateDeviceAppDelegate
@synthesize window;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
myViewController *obj =[[myViewController alloc]init];
[window addSubview:obj.view];
    
    [self.window makeKeyAndVisible];
    
    return YES;
}
Step 5: Deploy this application into the iPhone device and touch the screen you will see that the when you touch the screen the device vibrates and displays the alert view, and if you need any guidance in deploying the app then refer this post. 

No comments:

Post a Comment