Tuesday, March 16, 2010

Beginning UIAccelerometer

UIaccelerometer automatically detects when the user has moved his/her iphone. It measures on 3 axis planes, and they are the following.

X: When the user moves iphone left/right
Y: When the user moves iphone forward/backward
Z: When the user moves the iphone up/down

# Create your Viewcontroller.h file

#import

//sensitivity
#define kAccelerometerFrequency 40

@interface simpleTiltViewController : UIViewController {

CGPoint deviceTilt;
}

@end

# Create your ViewController.m file

#import "simpleTiltViewController.h"

@implementation simpleTiltViewController

- (void)viewDidLoad {
[super viewDidLoad];



[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}

// UIAccelerometerDelegate method, called when the device accelerates.
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// Update the accelerometer graph view
deviceTilt.x = acceleration.x;
deviceTilt.y = acceleration.y;
}

@end


Use these two files in your project

Accessing iphone address book

Create the Project

In Xcode, create a new project from the View Based Application template. Save the project as QuickStart. The next step is to add the frameworks you will need. First, go to your project window and find the target named QuickStart in the Targets group. Open its info panel (File > Get Info) and, in the General tab, you see a list of linked libraries. Add the Address Book and Address Book UI frameworks, by clicking the plus button and selecting them from the list.

Important: The project will fail to build if the Address Book and Address Book UI frameworks are not properly included.

Lay Out the View

Next, lay out the interface for your project. In the Resources folder of the project window, open the Interface Builder nib file named QuickStartViewController.xib. Add a button and two text labels to the view by dragging them in from the library. See the Figure below

project.h file

#import

#import

#import





@interface QuickStartViewController : UIViewController {

IBOutlet UILabel *firstName;

IBOutlet UILabel *lastName;

}

@property (nonatomic, retain) UILabel *firstName;

@property (nonatomic, retain) UILabel *lastName;



- (IBAction)showPicker:(id)sender;



@end


project.m file
#import "QuickStartViewController.h"



@implementation QuickStartViewController



@synthesize firstName;

@synthesize lastName;





- (IBAction)showPicker:(id)sender {

ABPeoplePickerNavigationController *picker =

[[ABPeoplePickerNavigationController alloc] init];

picker.peoplePickerDelegate = self;



[self presentModalViewController:picker animated:YES];

[picker release];

}

continue.. in same file
implementing the delegate protocol

- (BOOL)peoplePickerNavigationController:

(ABPeoplePickerNavigationController *)peoplePicker

shouldContinueAfterSelectingPerson:(ABRecordRef)person {



NSString* name = (NSString *)ABRecordCopyValue(person,

kABPersonFirstNameProperty);

// Do this during development
// Makes Program easier to debug

NSLog(name);
self.firstName.text = name;

[name release];



name = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

self.lastName.text = name;

[name release];



[self dismissModalViewControllerAnimated:YES];



return NO;

}
// So that implementation of delgate protocol doesn't remain incomplete

- (BOOL)peoplePickerNavigationController:

(ABPeoplePickerNavigationController *)peoplePicker

shouldContinueAfterSelectingPerson:(ABRecordRef)person

property:(ABPropertyID)property

identifier:(ABMultiValueIdentifier)identifier{

return NO;

}

// For handling the screen rotation
- (BOOL)shouldAutorotateToInterfaceOrientation:

(UIInterfaceOrientation)interfaceOrientation {

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}





- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}


//Finally, release allocated memory.

- (void)dealloc {

[firstName release];

[lastName release];

[super dealloc];

}



@end

Now Easiest Part


Make connections in Interface Builder.

Open .nib file for the Project/ViewController;

Control-click (or right-click) on File’s Owner and connect the outlets for firstName and lastName from File’s Owner to the first name and last name labels. Control-click on the “Tap Me!” button and connect the Touch Up Inside outlet from the button to File’s Owner, selecting showPicker as its action.

// Now what are you waiting for? Run the application