登录界面制作以及设备版本号上传
#import <UIKit/UIKit.h>
#import "SRWebSocket.h"
@interface ViewController : UIViewController<UITextFieldDelegate,SRWebSocketDelegate>
//- (IBAction)SaveLogin:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIImageView *logoImage;
- (IBAction)loginbuttonClick:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
//@property (weak, nonatomic) IBOutlet UIButton *SaveLogin;
@property (weak, nonatomic) IBOutlet UILabel *lbuser;
@property (weak, nonatomic) IBOutlet UILabel *lbpassword;
@end
#import "ViewController.h"
#import "ASIHTTPRequest.h"
#import "MD5.h"
#import "SVProgressHUD.h"
#import "Reachability.h"
#import "UIColor+Extensions.h"
#import "comm.h"
#import "UIDevice+IdentifierAddition.h"
#import <sys/utsname.h>
@interface ViewController ()
{
UIImageView *slashimage;
}
@property (weak, nonatomic) IBOutlet UITextView *serverMessage;
@property (weak, nonatomic) IBOutlet UITextField *input;
@property (weak, nonatomic) IBOutlet UILabel *connectStatus;
@property (nonatomic, readwrite) SRWebSocket *warbleSocket;
@property (nonatomic, readwrite) BOOL socketReady;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
CGSize viewSize = [[UIScreen mainScreen] bounds].size;
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg.png"]];
//[image setFrame:self.view.frame];
[image setFrame:CGRectMake(0, 0, viewSize.width, viewSize.height)];
[self.view addSubview:image];
[self.view sendSubviewToBack:image];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:) name:UITextFieldTextDidChangeNotification object:nil];
self.userName.delegate = self;
self.password.delegate = self;
[self.navigationController.navigationBar setHidden:YES];
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
NSString* strUid = [pref stringForKey:@"uid"];
NSString* strPwd = [pref stringForKey:@"pwd"];
self.userName.text = strUid;
self.password.text = strPwd;
self.userName.textColor = [UIColor colorWithHexString:@"767676"];
self.password.textColor = [UIColor colorWithHexString:@"767676"];
if ([self.userName.text isEqualToString:@""] || [self.password.text isEqualToString:@""])
{
[self.loginButton setEnabled:FALSE];
self.loginButton.titleLabel.textColor = [UIColor grayColor];
}
else
{
[self.loginButton setEnabled:TRUE];
self.loginButton.titleLabel.textColor = [UIColor whiteColor];
}
self.lbuser.backgroundColor = [UIColor colorWithHexString:@"b5b5b5"];
self.lbpassword.backgroundColor = [UIColor colorWithHexString:@"b5b5b5"];
self.loginButton.layer.borderWidth = 1.0f;
self.loginButton.layer.borderColor = [[UIColor whiteColor] CGColor];
self.loginButton.layer.cornerRadius = 4.0;
self.loginButton.titleLabel.textColor = [UIColor whiteColor];
self.loginButton.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.15];
//just test
//[self performSegueWithIdentifier:@"gotoHome" sender:self.loginButton];
// slashimage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Default-568h@2x.png"]];
// [slashimage setFrame:self.view.frame];
// [self.view addSubview:slashimage];
//
// [self performSelector:@selector(hideSlashImage) withObject:nil afterDelay:1.0];
[pref setValue:@"" forKey:@"CurrentChatContact"];
}
- (void)hideSlashImage
{
[UIView animateWithDuration:0.8 animations:^{
slashimage.alpha = 0.0;
} completion:^(BOOL finished) {
[slashimage removeFromSuperview];
}];
}
- (void) viewWillAppear:(BOOL)animated
{
CGSize viewSize = [[UIScreen mainScreen] bounds].size;
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
NSString* strUid = [pref stringForKey:@"uid"];
NSString* strPwd = [pref stringForKey:@"pwd"];
self.userName.text = strUid;
self.password.text = strPwd;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(applicationDidBecomeActiveNotification:)
name:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];
}
- (void) viewWillDisappear:(BOOL)animated
{
}
- (void)applicationDidBecomeActiveNotification:(NSNotification *)notification {
// Do something here
[self.userName resignFirstResponder];
[self.password resignFirstResponder];
}
- (void)viewDidAppear:(BOOL)animated
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSString*) machineName
{
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
}
- (IBAction)loginbuttonClick:(id)sender
{
NSString *strBundleVersion = [NSString stringWithFormat:@"%@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]];
NSString *tmpmodel = [[UIDevice currentDevice] model];
// NSString* model = [tmpmodel stringByReplacingOccurrencesOfString:@" " withString:@"_"]; //不能有空格
NSString *deviceid = [[UIDevice currentDevice] uniqueGlobalDeviceIdentifier];
NSString *version = [[UIDevice currentDevice] systemVersion];
NSString *devicetype = [[NSString alloc] initWithFormat:@"%@_%@_%@",deviceid, [self machineName],version];
//向管理平台登录,获取token
NSString *strUrl = [NSString stringWithFormat:@"%@/login?tel=%@&pwd=%@&devicetype=%@&vs=%@",WEB_SERVER_HOST,
self.userName.text,[self.password.text MD5EncodedString],devicetype,strBundleVersion];
NSLog(@"%@",strUrl);
NSString *strMd5 = [NSString stringWithMD5EncodedString:@"19"];
NSURL *url = [NSURL URLWithString:strUrl];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
// Use when fetching text data
NSString *responseString = [request responseString];
NSLog(@"login result:%@",responseString);
NSError *error = nil;
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
NSString* msgCode = [dict objectForKey:@"code"];
if ([msgCode isEqualToString:@"200"]) {
NSLog(@"login success!");
NSString* token = [dict objectForKey:@"token"];
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
[pref setValue:token forKey:@"logintoken"];
[pref setValue:self.userName.text forKey:@"uid"];
[pref setValue:self.password.text forKey:@"pwd"];
[pref setBool:TRUE forKey:@"login"];
//NSString *msgResult = [[dict objectForKey:@"result"] description];
[pref setValue:responseString forKey:@"loginresult"];
//[SVProgressHUD dismiss];
[self.userName resignFirstResponder];
[self.password resignFirstResponder];
[self performSegueWithIdentifier:@"gotoHome" sender:self.loginButton];
}
else {
[SVProgressHUD showErrorWithStatus:@"用户名或密码有误,请重新输入。"];
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
[pref setBool:FALSE forKey:@"login"];
return;
}
NSLog(@"msgCode is %@",msgCode);
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"login result:%@",error.description);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"请检查您的网络后重试!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}];
[request startAsynchronous];
}
#pragma mark - 键盘处理
#pragma mark 键盘即将显示
- (void)keyBoardWillShow:(NSNotification *)note{
CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat ty = self.view.frame.size.height - rect.size.height;
CGFloat shift = ty - self.loginButton.frame.origin.y-self.loginButton.frame.size.height;
[UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, shift);
}];
}
#pragma mark 键盘即将退出
- (void)keyBoardWillHide:(NSNotification *)note{
[UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
}
- (void)textFieldChanged:(NSNotification *)note{
if ([self.userName.text isEqualToString:@""] || [self.password.text isEqualToString:@""])
{
[self.loginButton setEnabled:FALSE];
self.loginButton.titleLabel.textColor = [UIColor grayColor];
}
else
{
[self.loginButton setEnabled:TRUE];
self.loginButton.titleLabel.textColor = [UIColor whiteColor];
}
}
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
//默认不跳转,等待登陆结果
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
BOOL blogin = [pref boolForKey:@"login"];
if (blogin) {
return TRUE;
}
return FALSE;
}
// 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