ios+php Register Login System iOS part (next)

Source: Internet
Author: User
Tags reserved

Then on the "ios+php Register Login System PHP part (above)" to learn

3.iOS part

The last time we finished writing the database section and the PHP section we'll finish the iOS section this time.
First of all, in the storyboard in a rush, into the following figure.
You can first enter the username and password in the text field to facilitate debugging later.

3.1 Login Section Code

Create a new uiviewcontroller named Registviewcontroller (for registered users, Viewcontroller for login).
In the ViewController.h Importregistviewcontroller
#import "RegistViewController.h"

Then set the control name in the login interface to write the user name set to Txtuser, the password's control name set to Txtpwd, and determine the button's method name called
Loginclick, the Registration button's method name is Registbutton.
and start writing code in VIEWCONTROLLER.M.

//
Viewcontroller.m
Ioslogin
//
Created by Cao Shu on 16/2/25.
COPYRIGHT:EMOJI:2016 year Caohan. All rights reserved.
//
#import "ViewController.h"
@interface Viewcontroller ()
@property (Weak, nonatomic) Iboutlet Uitextfield *txtuser;
@property (Weak, nonatomic) Iboutlet Uitextfield *txtpwd;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Do no additional setup after loading the view, typically from a nib.
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any of the can is recreated.
}
-(Ibaction) Loginclick: (ID) Sender {
Go to space before and after
NSString *username = [_txtuser.text stringbytrimmingcharactersinset:[nscharacterset WhitespaceCharacterSet]];
NSString *userpwd = [_txtpwd.text stringbytrimmingcharactersinset:[nscharacterset WhitespaceCharacterSet]];
Nsdictionary *jsondic = [self getjsondata:username userpwd:userpwd];
nsstring* Loginflag = [jsondic objectforkey:@ "Loginflag"];
NSLog (@ "%@", Loginflag);
[Self aletrinfo:loginflag];
}
-(Ibaction) Registbutton: (ID) Sender {
Uistoryboard *storboard = Self.storyboard;
Registviewcontroller *VC2 = [Storboard instantiateviewcontrollerwithidentifier:@ "VC2"];
[Self PRESENTVIEWCONTROLLER:VC2 animated:yes completion:nil];
}
Used to request PHP to get JSON
-(Nsdictionary *) Getjsondata: (NSString *) user_name userpwd: (NSString *) user_pwd {
Nserror *error;
NSString *urlstring = [NSString stringwithformat:@ "Http://192.168.1.106/iosLogin/index.php?action=login&user_ name=%@&user_pwd=%@ ", user_name,user_pwd];
Load a Nsurl Object
Nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:urlstring]];
Put the requested URL data into the NSData object
NSData *response = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];
IOS5 the parsing class nsjsonserialization from the response to the data in the dictionary
Nsdictionary *jsondic = [nsjsonserialization jsonobjectwithdata:response options:nsjsonreadingmutableleaves Error: &error];
NSLog (@ "The data received is%@", jsondic);
Returnjsondic;
}
Pop-up information
-(void) Aletrinfo: (NSString *) loginflag{
Uialertview *alert = [[Uialertview alloc]init];
[Alert settitle:@ "hint"]; [Alert Setdelegate:nil];
[Alert addbuttonwithtitle:@ "OK"];
if ([Loginflag isequal: @ "0"]) {
[Alert setmessage:@ "Account or password error"];
}
if ([Loginflag isequal:@ "1"]) {
[Alert setmessage:@ "landed successfully"];
}
[Alert show];
}
@end

In the registration button to jump in front of the field, to first stroyboard in the registration interface Stroyboard ID set to VC2 before you can jump.

NSString *urlstring = [NSString stringwithformat:@ "Http://192.168.1.106/iosLogin/index.php?action=login&user_ name=%@&user_pwd=%@ ", user_name,user_pwd];

Where the 192.168.1.106 can write localhost can also write their own IP address.
Write here you can first debug the login. The following registration user code is similar to this here.

3.2 Registration Interface Code
Import ViewController.h in registViewCongroller.h first
#import "ViewController.h"
Then there is the code in the REGISTVIEWCONTROLLER.M.

//
Registviewcontroller.m
Ioslogin
//
Created by Cao Shu on 16/2/27.
Copyright 2016 Caohan. All rights reserved.
//
#import "RegistViewController.h"
@interface Registviewcontroller ()
@property (Weak, nonatomic) Iboutlet Uitextfield *txtuser;
@property (Weak, nonatomic) Iboutlet Uitextfield *txtpwd;
@end
@implementation Registviewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Do no additional setup after loading the view.
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any of the can is recreated.
}
This is the registration button.
-(Ibaction) Registbutton: (ID) Sender {
NSString *username = [_txtuser.text stringbytrimmingcharactersinset:[nscharacterset WhitespaceCharacterSet]];
NSString *userpwd = [_txtpwd.text stringbytrimmingcharactersinset:[nscharacterset WhitespaceCharacterSet]];
Nsdictionary *jsondic = [self getjsondata:username userpwd:userpwd];
nsstring* Registflag = [jsondic objectforkey:@ "Registflag"];
NSLog (@ "%@", Registflag);
[Self aletrinfo:registflag];
}
This is the Back button.
-(Ibaction) Returnbutton: (ID) Sender {
[Self dismissmodalviewcontrolleranimated:yes];
}
-(Nsdictionary *) Getjsondata: (NSString *) user_name userpwd: (NSString *) user_pwd {
Nserror *error;
NSString *urlstring = [NSString stringwithformat:@ "Http://192.168.1.106/iosLogin/index.php?action=regist&user_ name=%@&user_pwd=%@ ", user_name,user_pwd];
Load a Nsurl Object
Nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:urlstring]];
Put the requested URL data into the NSData object
NSData *response = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];
IOS5 the parsing class nsjsonserialization from the response to the data in the dictionary
Nsdictionary *jsondic = [nsjsonserialization jsonobjectwithdata:response options:nsjsonreadingmutableleaves Error: &error];
NSLog (@ "The data received is%@", jsondic);
Returnjsondic;
}
-(void) Aletrinfo: (NSString *) registflag{
Uialertview *alert = [[Uialertview alloc]init];
[Alert settitle:@ "hint"]; [Alert Setdelegate:nil];
[Alert addbuttonwithtitle:@ "OK"];
if ([Registflag isequal: @ "0"]) {
[Alert setmessage:@ "user name already exists"];
}
if ([Registflag isequal:@ "1"]) {
[Alert setmessage:@ "registered successfully"];
}
[Alert show];
}
@end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.