# 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.
[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (keyboardwillshow :) name: uikeyboardwillshownotification object: Nil];
[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (keyboardwillhide :) name: uikeyboardwillhidenotification object: Nil];
[[Nsicationcenter center defacenter center] 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 isstring tostring: @ ""] | [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 authentication mseguewithidentifier: @ "gotohome" Sender: Self. loginbutton];
// Slashimage = [[uiimageview alloc] initwithimage: [uiimage imagenamed: @ "[email protected]"];
// [Slashimage setframe: Self. View. Frame];
// [Self. View addsubview: slashimage];
//
// [Self defined mselector: @ 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;
[[Nsicationicationcenter defacenter center]
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: @ "_"]; // spaces are not allowed.
Nsstring * DeviceID = [[uidevice currentdevice] uniqueglobaldeviceidentifier];
Nsstring * version = [[uidevice currentdevice] systemversion];
Nsstring * devicetype = [[nsstring alloc] initwithformat: @ "% @ _ % @", DeviceID, [self machinename], Version];
// Log on to the management platform and obtain the 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: @ "logtailken"];
[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 defined mseguewithidentifier: @ "gotohome" Sender: Self. loginbutton];
}
Else {
[Svprogresshud showerrorwithstatus: @ "the user name or password is incorrect. Please enter it again. "];
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: @ "check your network and try again! "Delegate: Nil cancelbuttontitle: @" OK "otherbuttontitles: Nil];
[Alert show];
}];
[Request startasynchronous];
}
# Pragma mark-keyboard processing
# The Pragma mark Keyboard will be displayed soon
-(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 );
}];
}
# The Pragma mark keyboard is about to exit
-(Void) keyboardwillhide :( nsnotification *) Note {
[Uiview animatewithduration: [Note. userinfo [uikeyboardanimationdurationuserinfokey] doublevalue] animations: ^ {
Self. View. Transform = cgaffinetransformidentity;
}];
}
-(Void) textfieldchanged :( nsnotification *) Note {
If ([self. username. Text isstring tostring: @ ""] | [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) shouldw.mseguewithidentifier :( nsstring *) identifier sender :( ID) sender
{
// No jump by default, waiting for login results
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