Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "LogIn.h"
- #import "BaseClass.h"
- #import "XMLReader.h"
- #import "QuestionOfTheDay.h"
- #import "MainView.h"
- #import "Config.h"
- @interface LogIn () <UITextFieldDelegate,MBProgressHUDDelegate,NSXMLParserDelegate>
- {
- // NSMutableDictionary * dictUser;
- BOOL varSuccess;
- }
- @property (weak, nonatomic) IBOutlet UITextField *txtUserName;
- @property (weak, nonatomic) IBOutlet UITextField *txtPassword;
- @property (weak, nonatomic) IBOutlet UIImageView *imgTxt1;
- @property (weak, nonatomic) IBOutlet UIImageView *imgTxt2;
- @property (weak, nonatomic) IBOutlet UILabel *lblVersion;
- @property (retain, nonatomic) NSURLConnection *connection;
- @property (retain, nonatomic) NSMutableData *receivedData;
- @end
- @implementation LogIn
- -(BOOL)textFieldShouldReturn:(UITextField *)textField
- {
- [textField resignFirstResponder];
- return YES;
- }
- -(void)textFieldDidBeginEditing:(UITextField *)textField
- {
- if(textField==self.txtUserName)
- [self.imgTxt1 setImage:[UIImage imageNamed:@"box_selected.png"]];
- else
- [self.imgTxt2 setImage:[UIImage imageNamed:@"box_selected.png"]];
- }
- -(void)textFieldDidEndEditing:(UITextField *)textField
- {
- if(textField==self.txtUserName)
- [self.imgTxt1 setImage:[UIImage imageNamed:@"box_nonselected.png"]];
- else
- [self.imgTxt2 setImage:[UIImage imageNamed:@"box_nonselected.png"]];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
- self.lblVersion.text = [NSString stringWithFormat:@"Version : %@",version];
- NSDictionary * dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"loginUser"];
- if(dict)
- [self gotoMainView:dict animated:NO];
- }
- - (IBAction)clickHelp:(id)sender {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://support.ebmappsllc.com/"]];
- }
- - (IBAction)clickRegister:(id)sender {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://register.ebmappsllc.com/"]];
- }
- -(void)gotoMainView:(NSDictionary*)dict animated:(BOOL)animated
- {
- UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
- NSDate *fromDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"questionDate"];
- NSLog(@"%@", fromDate);
- if(!fromDate) // if first time
- {
- QuestionOfTheDay *yourViewController = (QuestionOfTheDay *) [storyboard instantiateViewControllerWithIdentifier:@"QuestionOfTheDay"];
- yourViewController.dictUser=dict;
- [self.navigationController pushViewController:yourViewController animated:animated];
- }
- else
- {
- NSDateComponents *difference = [[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:fromDate toDate:[NSDate date] options:0];
- if([difference day]>0)
- {
- QuestionOfTheDay *yourViewController = (QuestionOfTheDay *) [storyboard instantiateViewControllerWithIdentifier:@"QuestionOfTheDay"];
- yourViewController.dictUser=dict;
- [self.navigationController pushViewController:yourViewController animated:animated];
- }
- else
- {
- MainView *yourViewController = (MainView *) [storyboard instantiateViewControllerWithIdentifier:@"MainView"];
- yourViewController.dictUser=dict;
- [self.navigationController pushViewController:yourViewController animated:animated];
- }
- }
- }
- -(void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- self.txtUserName.text=@"";
- self.txtPassword.text=@"";
- }
- - (IBAction)clickLogIn:(id)sender {
- [self.txtUserName resignFirstResponder];
- [self.txtPassword resignFirstResponder];
- if(self.txtUserName.text.length==0||self.txtPassword.text.length==0)
- {
- [BaseClass showText:@"Some field missing." onView:self.view];
- return;
- }
- if(![BaseClass isNetworkReachableOnView:self.view])
- return ;
- MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
- [self.view addSubview:HUD];
- HUD.delegate = self;
- HUD.labelText = @"Logging In, Please wait.";
- [HUD showWhileExecuting:@selector(getResponse) onTarget:self withObject:nil animated:NO];
- }
- - (void)getResponse {
- // NSString *currentDeviceId = [[[UIDevice currentDevice] identifierForVendor]UUIDString];
- NSString *registrationID = [[NSUserDefaults standardUserDefaults]stringForKey:@"registration_id"];
- NSString *login_url = [serviceBaseURL stringByAppendingFormat:@"CheckUser?userName=%@&password=%@&deviceID=%@&phoneType=%@",self.txtUserName.text,self.txtPassword.text,registrationID,@"iOS"];
- NSString *loginResponse = [BaseClass requestWithURL:[NSString stringWithFormat:@"%@",login_url] onView:self.view];
- // NSString * response = [BaseClass requestWithURL:[NSString stringWithFormat:@"http://prediabetes.ebmappsllc.com/Service.asmx/CheckUser?userName=%@&password=%@&deviceID=%@&phoneType=%@", self.txtUserName.text,self.txtPassword.text,currentDeviceId,@"iOS"] onView:self.view];
- //
- NSError *error = nil;
- NSDictionary *dict = [XMLReader dictionaryForXMLData:[loginResponse dataUsingEncoding:NSASCIIStringEncoding] options:XMLReaderOptionsProcessNamespaces error:&error];
- NSLog(@"%@",dict);
- NSString * stringResponse = [[dict objectForKey:@"string"] objectForKey:@"text"];
- NSString * str1 = [stringResponse substringFromIndex:[stringResponse rangeOfString:@"<Success>"].location+9];
- if([[str1 substringToIndex:[str1 rangeOfString:@"</Success>"].location] isEqualToString:@"Y"])
- {
- NSMutableDictionary *dictUser = [NSMutableDictionary dictionary];
- [dictUser setObject:self.txtUserName.text forKey:@"UserName"];
- [dictUser setObject:self.txtPassword.text forKey:@"Password"];
- [dictUser setObject:[str1 substringToIndex:[str1 rangeOfString:@"</UserId>"].location] forKey:@"UserId"];
- str1 = [stringResponse substringFromIndex:[stringResponse rangeOfString:@"<UserId>"].location+8];
- [dictUser setObject:[str1 substringToIndex:[str1 rangeOfString:@"</UserId>"].location] forKey:@"UserId"];
- str1 = [stringResponse substringFromIndex:[stringResponse rangeOfString:@"<UserGender>"].location+12];
- [dictUser setObject:[str1 substringToIndex:[str1 rangeOfString:@"</UserGender>"].location] forKey:@"UserGender"];
- str1 = [stringResponse substringFromIndex:[stringResponse rangeOfString:@"<ActivityLevel>"].location+15];
- [dictUser setObject:[str1 substringToIndex:[str1 rangeOfString:@"</ActivityLevel>"].location] forKey:@"ActivityLevel"];
- str1 = [stringResponse substringFromIndex:[stringResponse rangeOfString:@"<HeightInInch>"].location+14];
- [dictUser setObject:[str1 substringToIndex:[str1 rangeOfString:@"</HeightInInch>"].location] forKey:@"HeightInInch"];
- str1 = [stringResponse substringFromIndex:[stringResponse rangeOfString:@"<WeightInLB>"].location+12];
- [dictUser setObject:[str1 substringToIndex:[str1 rangeOfString:@"</WeightInLB>"].location] forKey:@"WeightInLB"];
- NSLog(@"user %@", dictUser);
- [[NSUserDefaults standardUserDefaults] setObject:dictUser forKey:@"loginUser"];
- [[NSUserDefaults standardUserDefaults] synchronize];
- [self registerClient];
- [self gotoMainView:dictUser animated:YES];
- }
- else
- {
- [self performSelectorOnMainThread:@selector(showAlert:) withObject:@"Login Failed." waitUntilDone:YES];
- }
- }
- -(void)registerClient{
- NSError *error = nil;
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- NSString *userID =[[defaults objectForKey:@"loginUser"]objectForKey:@"UserId"];
- NSDictionary *userDetail = [[NSDictionary alloc]initWithObjectsAndKeys:@"iOS",@"PhoneType",[defaults objectForKey:@"registration_id"],@"DeviceID",userID,@"ID", nil];
- NSMutableDictionary *registerDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:[defaults objectForKey:@"registration_id"],@"Handle",userDetail,@"UserDetails", nil];
- NSLog(@"%@",userDetail);
- NSLog(@"%@",registerDict);
- NSData *registerJson = [NSJSONSerialization dataWithJSONObject:registerDict options:0 error:&error];
- NSLog(@"%@",registerJson);
- NSString * registerStr =[NSString stringWithFormat:@"%@",registerDict];
- NSString *postLength = [NSString stringWithFormat:@"%lu", [registerStr length]];
- // Create the request.
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://ebmprediabetesnothubtestwebapp.azurewebsites.net/register/mobile/client"]];
- // Specify that it will be a POST request
- request.HTTPMethod = @"POST";
- // This is how we set header fields
- [request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
- [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
- // Convert your data and set your request's HTTPBody property
- NSData *requestBodyData = [registerStr dataUsingEncoding:NSUTF8StringEncoding];
- request.HTTPBody = requestBodyData;
- // Create url connection and fire request
- NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- if(conn)
- NSLog(@"Connection is Successfull");
- else
- NSLog(@"Connection Failed");
- [conn start];
- }
- /*
- this method might be calling more than one times according to incoming data size
- */
- -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
- [self.receivedData appendData:data];
- }
- /*
- if there is an error occured, this method will be called by connection
- */
- -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
- NSLog(@"%@" , error);
- }
- /*
- if data is successfully received, this method will be called by connection
- */
- -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
- //initialize convert the received data to string with UTF8 encoding
- NSString *registerResponse = [[NSString alloc] initWithData:self.receivedData
- encoding:NSUTF8StringEncoding];
- NSLog(@"Response Received %@" , registerResponse);
- }
- -(void)showAlert:(NSString*)string
- {
- [BaseClass showText:string onView:self.view];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
Add Comment
Please, Sign In to add comment