Last active
August 29, 2015 14:14
-
-
Save amannayak0007/fdc53f29b2c8e3bff616 to your computer and use it in GitHub Desktop.
Viewcontroller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
#import "NNPMasterViewController.h" | |
@interface ViewController () | |
{ | |
NSInteger success; | |
} | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
- (IBAction)sigininClicked:(id)sender { | |
success = 0; | |
@try { | |
if([[self.txtUsername text] isEqualToString:@""] || [[self.txtPassword text] isEqualToString:@""] ) { | |
[self alertStatus:@"Please enter Email and Password" :@"Sign in Failed!" :0]; | |
} else { | |
NSURL *url=[NSURL URLWithString: [[NSString alloc] initWithFormat:@"https://www.90degreeams.com/in/TodoLogin.ashx?email=%@&pass=%@",[self.txtUsername text],[self.txtPassword text]]]; | |
NSLog(@"PostData: %@",url); | |
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; | |
[request setURL:url]; | |
[request setHTTPMethod:@"POST"]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; | |
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; | |
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; | |
NSError *error = [[NSError alloc] init]; | |
NSHTTPURLResponse *response = nil; | |
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; | |
NSLog(@"Response code: %ld", (long)[response statusCode]); | |
if ([response statusCode] >= 200 && [response statusCode] < 300) | |
{ | |
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; | |
NSLog(@"Response ==> %@", responseData); | |
NSError *error = nil; | |
NSDictionary *jsonData = [NSJSONSerialization | |
JSONObjectWithData:urlData | |
options:NSJSONReadingMutableContainers | |
error:&error]; | |
success = [jsonData[@"UserId"] integerValue]; | |
NSLog(@"Success: %ld",(long)success); | |
if(success > 0) | |
{ | |
NSLog(@"Login SUCCESS"); | |
} else { | |
NSString *error_msg = (NSString *) jsonData[@"error_message"]; | |
[self alertStatus:error_msg :@"Sign in Failed!" :0]; | |
} | |
} else { | |
//if (error) NSLog(@"Error: %@", error); | |
[self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0]; | |
} | |
} | |
} | |
@catch (NSException * e) { | |
NSLog(@"Exception: %@", e); | |
[self alertStatus:@"Sign in Failed." :@"Error!" :0]; | |
} | |
if (success) { | |
[self performSegueWithIdentifier:@"login_success" sender:self]; | |
} | |
} | |
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { | |
if ([[segue identifier] isEqualToString:@"login_success"]) { | |
NNPMasterViewController *vc = [segue destinationViewController]; | |
[vc setItem:success]; | |
}} | |
- (void) alertStatus:(NSString *)msg :(NSString *)title :(int) tag | |
{ | |
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title | |
message:msg | |
delegate:self | |
cancelButtonTitle:@"Ok" | |
otherButtonTitles:nil, nil]; | |
alertView.tag = tag; | |
[alertView show]; | |
} | |
- (IBAction)backgroundTap:(id)sender { | |
[self.view endEditing:YES]; | |
} | |
- (IBAction)hideKeyboard:(id)sender { | |
[(UITextField *)sender resignFirstResponder]; | |
} | |
-(BOOL) textFieldShouldReturn:(UITextField *)textField { | |
[textField resignFirstResponder]; | |
return YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment