This tutorial will guide you to create a simple app with a Login screen which takes username and password from the user and then POST it to an url and parse the JSON response.
- Create a New project.
- Create the Screen(View).
- Declare and Connect the properties and functions to the UI elements.
- Set actions for Background Click and text field Return events.
- Post data to URL and parse the JSON response.
1. Create a New project.
1. Launch XCode .
2. Click: File->New Project…
and choose: Single View Application
3. Name the project: ‘LoginJson’ and you can give a file prefix also, here i have given “DK”.
When you choose Single View Application, XCode creates many files for you, among which the following files are of our interest:
Classes\DKViewControl.h
Classes\DKViewControl.m
Resources\DKViewControl.xib
the DKViewControl.xib is the design of the view
the other files are used for the class files for the view
and also XCode creates the view and puts it as the main screen when the program launches.
Got it by your older post.
Thaks
Hello Sir,
I have ran code and its given me following error pelase let me know what i have to do for it,
Error: Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” UserInfo=0x8da2030.
Thanks,
Use error_log(), instead of echo for all other outputs.
hi Dipin, my logintodb.php is below:
$host = “localhost”;
$user = “root”;
$pass = “root”;
$db=”iwll”;
$r = mysql_connect($host, $user, $pass);
if (!$r) {
echo “Could not connect to server\n”;
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo “Connection established\n”;
}
echo mysql_get_server_info() . “\n”;
$r2 = mysql_select_db($db);
if (!$r2) {
echo “Cannot select database\n”;
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo “database selected\n”;
}
$us=$_POST[‘username’];
$pw=$_POST[‘password’];
$result=mysql_query(“select username,pass from login where username=’$us’ and pass=’$pw'”);
$row=mysql_fetch_array($result);
if($row[“username”]==$us && $row[“pass”]==$pw)
{
echo ‘{“success”:1}’;
}
else
{
echo ‘{“success”:0,”error_message”:”Username and/or password is invalid.”}’;
}
Hi,
Are you ‘echo’ing anything before the ‘{“success”:1}’ ??
if i changed like that my output is:
2014-11-05 16:24:06.764 logintodb[90940:90b] Response ==> Connection established
5.5.38
database selected
{“success”:1}
2014-11-05 16:24:06.764 logintodb[90940:90b] Success: 0
and i got pop-up as Login failed
why i got initially success:1 and then success:0
pls explain me?
Change
echo ‘”success”:1’;
to
echo ‘{“success”:1}’;
nice tutorial.i have written my php code as:
if($row[“username”]==$u && $row[“pass”]==$pw)
{
echo ‘”success”:1’;
}
else
{
echo ‘{“success”:0,”error_message”:”Username and/or password is invalid.”}’;
}
but i got in console pane as:2014-11-04 12:06:14.701 logintodb[5003:90b] Response ==> Connection established
5.5.38
database selected
“success”:1
2014-11-04 12:06:14.701 logintodb[5003:90b] Success: 0
i dont know what it means?pls help me?
try 127.0.0.1:8888 instead of dipinkrishna.com
Excellent tutorial! I have tried with this code.i am using MAMP server.so i don’t know what i have to put here:
NSURL *url=[NSURL URLWithString:@”https://dipinkrishna.com/jsonlogin.php”];
could u pls help me?
If you are starting a new project, use storyboard.
xcode 6.0.1
Hey,
Which version of xcode are you using?
What do you mean by setting tab bar controller as the root view controller. Does it mean selecting this as a initial view controller? Are they same concept? How do you set a view controller as a root view controller? Could you provide sample code, please?
If you are not getting a response then it might be because your php file is throwing an error.
My latest tutorial (using swift) has DB connectivity.
Download the php source code from https://dipinkrishna.com/blog/2014/07/login-signup-screen-tutorial-xcode-6-swift-ios-8-json/
hi dipin,
thanx for your wonderful tutorial.It works for me.
but there is one problem, when i tried to change my php file code to get username and password from mysql database i got an error saying signin failed. will u pls send me a php code where mysql database connectivity is done and in that case if there is any change i would like to do in my objective C code to get response from edited php file becoz right now i am getting a response(NULL) and success:0.
thanks in advance.
THx in advance.
Its because, the server is not returning a valid json.