How to detect shakes in an iPhone Application?

This post deals with detecting shakes in an iPhone application. The shake API has 3 events which can be handled in your code

a) motionBegan:

b) motionEnded:

c) motionCancelled:

Now lets see how you can code these events in your application

1)Open Xcode, make a view based application and name it as shake.

2)Open shakeViewController.xib and populate it with a TextField and a UIDatePicker as shown below

3) Now open shakeViewController.h file. Add the following highlighted code

 

#import <UIKit/UIKit.h>
@interface ShakeViewController : UIViewController {
IBOutlet UITextField *textField;
IBOutlet UIDatePicker *datePicker;
}
@property (nonatomic, retain) UITextField *textField;
 @property (nonatomic, retain) UIDatePicker *datePicker;
-(IBAction) doneEditing: (id) sender;
@end

 

4) In the Interface Builder make the connections for textField and datePicker

5) Right click the textField and connect its Did End On Exit event to doneEditing: method as shown below

 

 

6) Now write the following highlighted code in the shakeViewController.m file

 

#import "ShakeViewController.h"
@implementation ShakeViewController
@synthesize textField, datePicker;
-(void) viewDidAppear:(BOOL)animated
{
[self.view becomeFirstResponder];
 [super viewDidAppear:animated];
}
-(IBAction) doneEditing: (id) sender
{
 [self.view becomeFirstResponder];
}
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeMotionShake )
 {
NSLog(@"motionBegan:");
}
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
 if (event.subtype == UIEventSubtypeMotionShake )
{
NSLog(@"motionCancelled:");
}
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
 {
 if (event.subtype == UIEventSubtypeMotionShake )
{
NSLog(@"motionEnded:");
}
}
-(void)dealloc
{
[textField release];
 [datePicker release];
[super dealloc];
}

 

7) Right click the classes group and choose add new file. Select the UIView subclass template.

 

8) Name the file as ShakeView.

9) Open ShakeView.m and add the following method

-(BOOL)canBecomeFirstResponder
{
 return YES;
}

 

10) In Interface Builder, select the View window and view its Identity Inspector window. Select its class name as ShakeView.

 

11) Save everything and then run the application.

12) To simulate shaking on simulator choose Hardware->Shake Gesture

13) Observe the console.

Note: You should have first responder in your view to fire the events. So we have set view to first responder in viewDidAppear: and then again when keyboard is dismissed we make view as first responder in doneEditing: method. By default your view will never be first responder so you will have to override the default canBecomeFirstResponder method to return a YES:

 

How to know which UIButton was pressed?

How to know which UIButton is pressed when

there are more than one UIButton in a view?

 

Suppose there are two buttons in a view

Now in the identity inspector window for button0 set the tag value=0

Set the tag value for button1 as 1 in the same way as for button0

 

Now here is the coding part

Link the following method with both the buttons in the Interface Builder

-(IBAction)action:(id)sender
{
if ([sender tag]==0) {
NSLog(@"button0");
}
else
{
NSLog(@"button1");
}
}

Here sender gets the identity of the button object which is pressed.

By using  [sender tag] we get to know which button was pressed.

Same thing applies for more than two buttons provided different tag values should be used for each UIButton object.

The new Xcode 4 – Intelligent enough!

The new Xcode 4 – Intelligent enough!

Apple has launched the new Xcode 4 for the iOS Application Developers. It is for sure that the developers are going to love it because of its awesome features. Lets have a look at some of its new features.

What’s new in Xcode 4

 

The tools in Xcode 4 are redesigned to work faster and smoother. It has become more helpful, and it not only tells you error but also fix it for you.

 

Some of the new features which you will notice at first….

 

Single Window

Yes, the Xcode 4 is a single window development environment which combines Interface builder in the same window in which you write code.

 

Interface Builder is Built-in

There is no seperate application called interface builder. Infact, interface builder is integrated with Xcode. Selecting a .xib file will directly open editor in Xcode. You can drag connections directly from UI design to source code. If your code is not ready with actions and outlets then just drag connection to empty space in source code and Xcode 4 will define outlets and actions for you.

 

Assistant

This is another useful addition in Xcode4. If you make some changes in a source file then assistant will help you in determining which other source files need to be checked, as you work.

 

Fix it

The Xcode 4 is an IDE which is intelligent enough, not only to report but also fix problem for you.

How to create iPhone/iPad applications and submit them to App Store

How to create iPhone/iPad applications and submit them to App Store

So today we are going to see how to create iPhone/iPad applications and then submit them to App Store. So basically we will be starting with the requirements first that we need to get us started.

Requirements for building iPhone application

 

Macintosh computer

You can build an iPhone application only on a Macintosh computer. Macintosh computers run Mac as their operating system, and are completely different from Windows. You cannot install Mac operating system on a Windows PC as it is hardware dependent. So guys if you own a Windows computer then get ready to shed some money for buying your new Mac. You will feel the difference between them from the day you start using Mac.

 

Xcode

Xcode is an integrated development environment developed by Apple. Applications for iPhone/iPad are developed using Xcode in Objective c language. You can download Xcode by registering on Apple’s developer website. Xcode is downloaded as a complete package which consists of a programming environment for writing codes, an Interface Builder for designing pages and an iPhone simulator for testing applications.

 

 

 

iOS Developer Program

Ok, this is not so good part as it involves some more expenditure after you buy your Macbook. Apple needs you to be enrolled in its developer program before you can start putting applications on app store for sale. There are two membership programs, depending on whether you are a company(iOS Developer program Enterprise – $299) or an individual(iOS Developer Program Standard – $99) you can select your membership. After you get registered you can submit your application for sale on app store.