ios login with php & NSUserDefaults : Application
 

Blog :: Application :: ios login with php & NSUserDefaults

LoginView.h

#import <UIKit/UIKit.h>

@interface LoginView : UIViewController
{
    IBOutlet UITextField *txtUsername;
    IBOutlet UITextField *txtPassword;
    UIActivityIndicatorView *activityView;
    UIView *loadingView;
    UILabel *loadingLabel;
}

@property(nonatomic,assign) NSMutableData *receivedData;

@property (nonatomic, retain) UIActivityIndicatorView *activityView;
@property (nonatomic, retain) UIView *loadingView;
@property (nonatomic, retain) UILabel *loadingLabel;


@end

LoginView.m

#import "Login.h"
#import "LoginInfoView.h"

@interface LoginView ()
{
    UIAlertView *loading;  
}

@end

@implementation LoginView

@synthesize receivedData;
@synthesize loadingView;
@synthesize loadingLabel;
@synthesize activityView;

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    NSUserDefaults *default1 = [NSUserDefaults standardUserDefaults];
    NSString *FUSERNAME = [default1 objectForKey:@"FUSERNAME"];
    NSString *FRUN = [default1 objectForKey:@"FRUN"];
    NSString *FCODE = [default1 objectForKey:@"FCODE"];
    NSLog(@"NSUserdefault Value Is: %@ %@ %@",FUSERNAME, FRUN, FCODE);//แสดงค่าของ NSUserDefaults

    
    if (FUSERNAME == nil) {

        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please Login" message:@"กรุณากรอก ชื่อผู้ใช้ และรหัสผ่าน" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login", nil];
        [alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
        
        [alert show];
        [alert release];
        
    } else {

        [self performSegueWithIdentifier:@"seguelogin9" sender:self];
        

    }

    
    
}

- (void)viewDidLoad
{ 
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    
    if ([title isEqualToString:@"Login"]) {
        UITextField *username = [alertView textFieldAtIndex:0];
        UITextField *password = [alertView textFieldAtIndex:1];
        txtUsername.text = [username text];
        txtPassword.text = [password text];
        
        
        //Show Progress Loading...
        loadingView = [[UIView alloc] initWithFrame:CGRectMake(75, 155, 170, 170)];
        loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        loadingView.clipsToBounds = YES;
        loadingView.layer.cornerRadius = 10.0;
        
        activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        activityView.frame = CGRectMake(65, 40, activityView.bounds.size.width, activityView.bounds.size.height);
        [loadingView addSubview:activityView];
        
        loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 115, 130, 22)];
        loadingLabel.backgroundColor = [UIColor clearColor];
        loadingLabel.textColor = [UIColor whiteColor];
        loadingLabel.adjustsFontSizeToFitWidth = YES;
        loadingLabel.textAlignment = UITextAlignmentCenter;
        loadingLabel.text = @"Loading...";
        [loadingView addSubview:loadingLabel];
        
        [self.view addSubview:loadingView];
        [activityView startAnimating];

        
        
        NSString *user = [username text];
        NSString *pass = [password text];
        NSLog(@"Echo Username: %@ and Password: %@", user, pass);
        
        NSString *post = [NSString stringWithFormat:@"sUsername=%@&sPassword=%@",user,pass];
        
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
        
        NSURL *url = [NSURL URLWithString:@"http://idealup.com/booking_server/php/checklogin.php"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
        
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];
        
        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        
        if (theConnection) {
            self.receivedData = [[NSMutableData data] retain];
        } else {
            UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"NSURLConnection" message:@"Failed in viewDidLoad" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [connectFailMessage show];
            [connectFailMessage release];
        }
        
        
        
    }
    
    if ([title isEqualToString:@"Cancel"]) {
        
        NSLog(@"Login Failed ");
    }
   
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [loading dismissWithClickedButtonIndex:0 animated:YES];
    
    if (receivedData) {
        id jsonObjects = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:nil];
        
        
        NSString *strStatus = [jsonObjects objectForKey:@"Status"];
        NSString *strFUSERNAME = [jsonObjects objectForKey:@"FUSERNAME"];

        
        NSLog(@"strStatus = %@",strStatus);
        NSLog(@"FUSERNAME = %@",strFUSERNAME);

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        [defaults setObject:strFUSERNAME forKey:@"FUSERNAME"];
        
        
        NSString *session = [defaults objectForKey:@"FNAME"];
        
        NSLog(@"Session = %@ : FCODE = %@",session,strFCODE);

        
        if ([strStatus isEqualToString:@"1"]) {
            
            NSLog(@"Success Login NSUserDefaults is: %@", FUSERNAME);
   
        } else {
            
            UIAlertView *error = [[UIAlertView alloc] initWithTitle:@":( Error" message:@"Error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [error show];
            
            [self.parentViewController.tabBarController setSelectedIndex:0];
        }
    }
    
    
   
    
    [connection release];
    [receivedData release];
}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    sleep(3);
    [receivedData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [connection release];
    [receivedData release];
    
    UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle:@"NSURLConnection" message:@"didFailWithError" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    
    [didFailWithErrorMessage show];
    [didFailWithErrorMessage release];
    
    NSLog(@"Connection Failed %@",[error localizedDescription]);
}







0 Comment

  • Have no comment.

Comment Blog

 
 
 
Share This Topic Login with Facebook