1#import"HMViewController.h"2#import"mbprogresshud+mj.h"3 4 @interface Hmviewcontroller ()5@property (Weak, nonatomic) Iboutlet Uitextfield *Usernamefield;6@property (Weak, nonatomic) Iboutlet Uitextfield *Pwdfield;7-(ibaction) login;8 @end9 Ten @implementation Hmviewcontroller One A - /** - * Login Logic the */ --(ibaction) login - { - //1. Form validation (input validation) +NSString *username =Self.usernameField.text; - if(Username.length = =0) {//no user name entered +[Mbprogresshud ShowError:@"Please enter user name"]; A return; at } - -NSString *pwd =Self.pwdField.text; - if(Pwd.length = =0) {//no password entered -[Mbprogresshud ShowError:@"Please enter your password"]; - return; in } - to //Bullet Box +[Mbprogresshud ShowMessage:@"Trying desperately to log in ..."]; - the //2. Send the request to the server (with the account number and password) * //POST request: Request body $ Panax Notoginseng //2.1. Set the request path -Nsurl *url = [Nsurl urlwithstring:@"Http://192.168.1.200:8080/MJServer/login"]; the + //2.2. Create a Request object ANsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];//The default is a GET request theRequest.timeoutinterval =5;//Set Request Timeout +Request. HttpMethod =@"POST";//Set as POST request - $ //tell the server client type by the request header $[Request SetValue:@"iOS"Forhttpheaderfield:@"user-agent"]; - - //set the request body theNSString *param = [NSString stringWithFormat:@"username=%@&pwd=%@", username, pwd]; -Request. Httpbody =[param datausingencoding:nsutf8stringencoding];Wuyi the //2.3. Sending the request -Nsoperationqueue *queue =[Nsoperationqueue Mainqueue]; Wu[Nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^ (NSURLResponse *response, NSData * Data, Nserror *connectionerror) {//called when the request is over (the server's data is received, the request fails) - //Hide Hud (Refresh UI, be sure to put it on the main thread, not on child threads) About [Mbprogresshud Hidehud]; $ - /** - Parse Data: - {"Error": "User name does not exist"} A {"Error": "Password is incorrect"} + {"Success": "Login Succeeded"} the */ - if(data) {//Request succeeded $Nsdictionary *dict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil]; theNSString *error = dict[@"Error"]; the if(Error) {//Logon Failure the [Mbprogresshud Showerror:error]; the}Else{//Login Successful -NSString *success = dict[@"Success"]; in [Mbprogresshud showsuccess:success]; the } the}Else{//request failed About[Mbprogresshud ShowError:@"the network is busy, please try again later"]; the } the }]; the } + -@end
iOS Network nsurlconnection POST