| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- //
- // LoginViewController.m
- // RedAnt ERP Mobile
- //
- // Created by Ray on 14-6-20.
- // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
- //
- #import "LoginViewController.h"
- #import "RAUtils.h"
- #import "ACNetwork.h"
- #import "AppDelegate.h"
- //#import "RetrievePassViewController.h"
- //#import "UILabel+FontAppearance.h"
- //#import "OLDataProvider.h"
- //#import "NotificationNameCenter.h"
- //#import "OfflineSettingViewController.h"
- //#import "MainViewController.h"
- //#import "WebViewController.h"
- @interface LoginViewController ()
- @property (strong, nonatomic) IBOutlet UIButton *requestLoginBtn;
- @end
- @implementation LoginViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.title=@"Sign in";//@"Granite Expo";
- self.showList = false;
-
-
- self.bb_close.image=[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
-
- [self.checkSavePassword setImage:[UIImage imageNamed:@"checkbox"] forState:UIControlStateNormal];
- [self.checkSavePassword setImage:[UIImage imageNamed:@"checkbox_check"] forState:UIControlStateSelected];
-
-
-
-
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- NSString * user = [AESCrypt decrypt:[defaults stringForKey:@"user"] password:@"usai"];
-
-
- NSString * password = [AESCrypt decrypt:[defaults stringForKey:@"password"] password:@"usai"];
-
- if(user.length>0&&password.length>0)
- {
- self.edituser.text=user;
- self.editpass.text=password;
- self.checkSavePassword.selected=true;
-
- }
- // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- // [defaults removeObjectForKey:@"EnableOfflineMode"];
- // [defaults setBool:self.checkOfflineMode.selected forKey:@"EnableOfflineMode"];
-
-
- [self.checkSavePassword addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
- // self.checkOfflineMode.tag=999;
-
- // self.logindata = [iSalesDB get_saveduser];
-
-
-
- UIButton* combobutton=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
- [combobutton setBackgroundImage:[UIImage imageNamed:@"combo_24"] forState:UIControlStateNormal];
- [combobutton addTarget:self action:@selector(LoginList:) forControlEvents:UIControlEventTouchUpInside];
-
- self.edituser.rightView = combobutton;
- self.edituser.rightViewMode=UITextFieldViewModeAlways;
-
-
- //
- // self.Loginlist.layer.borderColor = [UIColor lightGrayColor].CGColor;
- // self.Loginlist.layer.borderWidth = 1.0;
- // // self.Loginlist.layer.cornerRadius=15;
- // self.Loginlist.layer.masksToBounds=true;
- // Do any additional setup after loading the view.
- }
- - (void)LoginList:(UIButton *)sender {
- [self.edituser endEditing:true];
- self.showList= !self.showList;
- // self.Loginlist.hidden = !self.showList;
- // showList;
- }
- -(void)checkboxClick:(UIButton *)btn
- {
- btn.selected = !btn.selected;
- {
- if(!btn.selected)
- {
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- [defaults removeObjectForKey:@"user"];
- [defaults removeObjectForKey:@"password"];
- [defaults synchronize];
-
-
- NSString* encryptu=[AESCrypt encrypt:self.edituser.text password:@"usai"];
-
- // NSString* sql=[NSString stringWithFormat:@"delete from login_info where name='%@'",encryptu];
- // [iSalesDB execSql:sql];
- // self.logindata = [iSalesDB get_saveduser];
- // [self.Loginlist reloadData];
- }
-
- }
- }
- - (IBAction)CloseClick:(id)sender {
-
- [self dismissViewControllerAnimated:true completion:nil];
- if(self.returnValue)
- self.returnValue(false);
-
-
-
- }
- - (IBAction)onRetrievePassword:(id)sender {
- // RetrievePassViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"RetrievePassViewController" ];
- // // dvc
- // // UINavigationController* nv = self.navigationController;
- //
- // [self.navigationController pushViewController:vc animated:true];
- }
- - (IBAction)onLoginClick:(UIButton *)sender {
- NSString* user = self.edituser.text;
- NSString* password = self.editpass.text;
-
-
-
- // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- //
- // bool forcedownload = [defaults boolForKey:@"Forcedownload"];
-
- // if(forcedownload && self.checkOfflineMode.selected)
- // {
- // self.checkOfflineMode.selected = !self.checkOfflineMode.selected;
- //
- // [RAUtils alert_view:@"Detected incomplete sync, please finish sync first." title:@"Incomplete sync"];
- // return;
- // }
-
- if(user.length==0||password.length==0)
- {
- // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Error!" message:@"user or password can not be empty." delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil, nil];
- // // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
- //
- //
- // //[[UILabel appearanceWhenContainedIn:UIAlertView.class, nil] setAppearanceFont:[UIFont boldSystemFontOfSize:10]];
- // [alert show];
-
-
- [RAUtils alert_view:@"Username or Password cannot be blank." title:@"Cannot sign-in"];
- return;
- }
- DebugLog(@"Login... user= %@ ; password= %@",user,password);
-
-
-
-
- self.btnLogin.enabled = false;
- UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Sign in"];
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
-
- int ret=[ACNetwork Authorize:user password:password];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
- self.btnLogin.enabled = true;
-
- if(ret==RESULT_TRUE)
-
- {
-
- //
- // NSString* contactid=[self.content_data[indexPath.row] valueForKey:@"contact_id"] ;
- AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- // appDelegate.contact_id=contactid;
- // appDelegate.contact_name =[self.content_data[indexPath.row] valueForKey:@"name"];
-
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- [defaults removeObjectForKey:@"user"];
- [defaults removeObjectForKey:@"password"];
- NSString* encryptu=[AESCrypt encrypt:appDelegate.user password:@"usai"];
- NSString* encryptp=[AESCrypt encrypt:self.editpass.text password:@"usai"] ;
- if(self.checkSavePassword.selected)
- {
-
- [defaults setValue:encryptu forKey:@"user"];
- [defaults setValue:encryptp forKey:@"password"];
- // sqlite3 *db = [iSalesDB get_db];
- //
- // int count =[iSalesDB get_recordcount:db table:@"login_info" where:[NSString stringWithFormat:@"name='%@'",encryptu]];
- //
- // if(count==0)
- // {
- //
- // NSString * savelogin=[NSString stringWithFormat:@"insert into login_info(name,pwd) values('%@','%@')",encryptu,encryptp];
- // [iSalesDB execSql:savelogin db:db];
- // }
- // else
- // {
- // NSString * savelogin=[NSString stringWithFormat:@"update login_info set pwd='%@' where name='%@'",encryptp,encryptu];
- // [iSalesDB execSql:savelogin db:db];
- // }
- //
- // [iSalesDB close_db:db];
-
- // [defaults setBool:TRUE forKey:@"autologin"];
- }
- // else
- // {
- // NSString* sql=[NSString stringWithFormat:@"delete from login_info where name='%@'",encryptu];
- // [iSalesDB execSql:sql];
- // }
- [defaults synchronize];
-
- [self dismissViewControllerAnimated:YES completion:^{
-
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
-
- bool offline_dirty=[defaults boolForKey:@"OFFLINE_DIRTY"];
- // sqlite3* db = [iSalesDB get_db];
- // int offline_user_exist=[iSalesDB get_recordcount:db table:@"offline_login" where:[NSString stringWithFormat:@"username='%@'",appDelegate.user ]];
- //
- // [iSalesDB close_db:db];
-
-
-
- // else
- {
- if(self.returnValue)
- self.returnValue(true);
- // [[NSNotificationCenter defaultCenter] postNotificationName:LoginOK_HandleUrgencyFile_Notification object:nil];
- }
- }];
-
- // if (self.delegate && [self.delegate respondsToSelector:@selector(LoginViewControllerDidLogin:)]) {
- // [self.delegate LoginViewControllerDidLogin:self];
- // }
-
- // [[NSNotificationCenter defaultCenter] postNotificationName:User_LoginOK_Notification object:nil];
- }
- else
- {
- NSString* msg=nil;
-
- switch (ret) {
-
- case RESULT_NET_NOTAVAILABLE:
- msg = MSG_NET_NOTAVAILABLE;
- break;
-
-
- case RESULT_NET_ERROR:
- msg = MSG_NET_ERROR;
- break;
-
-
- case RESULT_FALSE:
- msg = MSG_USERAUTH_ERROR;
- break;
-
- case RESULT_VER_LOW:
- msg = MSG_VER_LOW;
- break;
-
- case RESULT_LOGIN_DEVICE:
- msg= MSG_LOGIN_DEVICE;
- break;
- default:
- break;
- }
-
- [RAUtils alert_view:msg title:@"Cannot sign-in"];
-
-
-
- }
-
-
- });
- });
-
-
-
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - Table view data source
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- {
- return 44;
-
-
- }
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return self.logindata.count;
-
-
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
-
- NSMutableDictionary* up = self.logindata[indexPath.row];
-
-
-
- NSString *CellIdentifier = @"LoginListCell";
- UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
- NSString* name=up[@"name"];
- cell.textLabel.text = name;
- return cell;
-
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
-
- // NSMutableDictionary* up = self.logindata[indexPath.row];
- // self.edituser.text = up[@"name"];
- // self.editpass.text = up[@"pwd"];
- //
- // self.showList=false;
- // self.Loginlist.hidden=true;
- // self.checkSavePassword.selected=true;
-
- }
- - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
- {
-
- return true;
- }
- - (void)textFieldDidBeginEditing:(UITextField *)textField
- {
-
- self.showList=false;
- // self.Loginlist.hidden=true;
-
- }
- #pragma mark - action
- //- (IBAction)requestLoginBtnClicked:(UIButton *)sender {
- //
- // WebViewController *webVC = [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewController"];
- // webVC.url = URL_REQUEST_LOGIN;
- // webVC.title = @"Request Login";
- // webVC.backItemDismiss = YES;
- //
- //
- // UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:webVC];
- // nav.modalPresentationStyle = UIModalPresentationFormSheet;
- //
- // [self presentViewController:nav animated:YES completion:nil];
- //
- //}
- @end
|