Tuesday, 28 June 2011

Login page Tutorial


This article shows how to create a Login Screen for iphone using Http request. In this article , you can easily create  your login Page.

Creating the project:----
1)  Launch XCode.
2)  Click on XCode File --> New Project
3)  Choose View Based Application
4)  Name of the Project --> LoginExample


Taking help of View Based Application , We refer to following Four files and Interface Builder (xib) :-

LoginExampleAppDelegate.h
LoginExampleAppDelegate.m
LoginExampleViewController.h
LoginExampleViewController.m
LoginExampleViewController.xib


Step 1 :---  Create  LoginExampleAppDelegate.h



Following is basic header file required to proceed ahead -

AppDelegate Header

#import <UIKit/UIKit.h>

@class LoginExampleViewController;

@interface LoginExampleAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    LoginExampleViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet LoginExampleViewController *viewController;

@end


Step 2 :-- Create LoginExampleAppDelegate.m

AppDelegate Main File

#import "LoginExampleAppDelegate.h"
#import "LoginExampleViewController.h"

@implementation LoginExampleAppDelegate

@synthesize window;
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}


- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}

@end


Step 3 :-- Create LoginExampleViewController.h

Now, we need to code our ViewController.

1. We have added two UITextField for username & password and a button (UIButton) for submit
2. Next, we add one Interface Builder Action (IBAction) method and add specified TextField and Button property.


#import <UIKit/UIKit.h>

@interface LoginExampleViewController : UIViewController {
IBOutlet UITextField *unameField;
IBOutlet UITextField *passwordField;
IBOutlet UIButton *loginButton;
NSMutableData *receiveData;

}

@property (nonatomic, retain) UITextField *unameField;
@property (nonatomic, retain) UITextField *passwordField;
@property (nonatomic, retain) UIButton *loginButton;
@property (nonatomic, retain) NSMutableData *receiveData;

-(IBAction)doLogin: (id)sender;


@end


Step 4 :-- Create LoginExampleViewController.m

Now, Open LoginExampleViewController.m file and implement the interface elements.

a) Here firstly import the LoginExampleViewController.h
b) Synthesize the elements which is specified in LoginExampleViewController.h properties.
c) Now implement the IBAction method.

In IBAction Method -->
 1. Using NSURL method :-
    Using NSURL method ,we can pass URL.


 2. Using NSURLRequest method :-
    Get the request (load the request) using NSURLRequest, using requestWithURL property.


 3. Using NSURLConnection method :-
    Using this method, we can establish connection, i.e. It supports to perform the loading of     a URL request.
       
Here we shall use some connection delegates method -->

I) -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data  {
}
II)  -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response  {
}

III)   -(void)connection didFinishLoading :(NSURLConnection *)connection {
}


LoginExampleViewController.m

#import "LoginExampleViewController.h"

@implementation LoginExampleViewController


@synthesize unameField;
@synthesize passwordField;
@synthesize loginButton;
@synthesize receiveData;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}

//Implement IBAction method
-(IBAction) doLogin: (id) sender
{
NSString *uname= unameField.text;
NSString *password= passwordField.text
NSURL *theURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8888/powerplay/insert2.php?name=%@&password=%@",uname, password]]; //Here you place your URL Link
NSURLRequest *req = [NSURLRequest requestWithURL:theURL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];
if (connection) {
NSLog(@"connection successful");
}
else {
NSLog(@"Failed");
}
}


// Implement Connection delegate

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
}
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[receiveData setLength:0];
// NSURL *theURL=[response URL];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"success",[receiveData length]);
[connection release];
[receiveData release];
}


// Implement TextField delegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}


// Implement TouchEvent


-(void)touchesBegan :(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
[unameField release];
[passwordField release];
    [super dealloc];
}




@end



Step 5 :-- Create  the LoginViewController.xib 

1. Open Resources --> LoginExampleViewController.xib.







2. Double click  on the LoginExampleViewController.xib.

3. First drag two label from the library (command+shift+l) and put it on the view. Change the label name.



4. Again drop and drag two textfield and label from library(command+shift+l)and put it on  the view. 

5. Now drag a button.



6. Here create a secure password using the textfield attributes(text Input Traits).



7. Link the button to the IBAction method and file's owner.






8. Link all textfield and its delegate.


I.


II.




9. Save Interface builder(IB) and close it and go back to the XCode.






Step 6 :-- Build and run your application…


In this application ,you insert username and password then press submit button . your username and password has been submitted on database.


Enjoy your Application!