For Swift tutorial, See: Swift + XCode 6 + iOS 8 + JSON
iOS Login Screen tutorial : XCode 5 + ios 7 + Storyboard + JSON
This tutorial will guide you to create a simple app with a Login screen which takes username and password from the user and then posts it to an url and parses the JSON response.
- Create a New project.
- Create the Screens and the Segue/transition.
- Declare and Connect the properties and actions 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.
- Launch XCode.
- Click: File->New Project… and choose: Single View Application
- Name your project, here i am using ‘LoginScreenTutorial’
When you choose Single View Application, XCode creates many files for you, among which the following files are of our interest:
ViewController.h
ViewController.m
Main.storyboard
You have to update the swift code to look for the right params passed by the server.
HI,
It works fine if i use your webservices. i do have another web service
“”,”error_message”=>””);
if ($con) {
//echo “connected” . “”;
} else {
//echo “not connected” . “”;
}
$json = array();
$username = $_GET[“username”];
$password = $_GET[“password”];
$image=mysqli_query($con,”Select * from login where username=’$username’ and password=’$password'”);
$row_count=mysqli_num_rows($image);
if ($row_count>0)
{
$data[“error_code”] =1000;
$data[“error_message”]=”Success”;
print json_encode($data);
}
else
{
$data[“error_code”] = 1001;
$data[“error_message”] = “Username or Password is wrong”;
print json_encode($data);
}
mysqli_close($con);
?>
it works fine in postman client
but not working in the simulator why?
can u help me?
Thanks in advance
Thanks !!
i followed many tutorial but your tutorial works ..
its easy to understand json
Thanks
thank you very much bro.
Nice Tutorial!!
Thanx!
Thanks for this tutorial. I was able to add a login screen to a existent project and it run smooth in the first try!
Hey,
Check out the swift tutorial.
https://dipinkrishna.com/blog/2014/07/login-signup-screen-tutorial-xcode-6-swift-ios-8-json/
At the end i have attached the php code which you can use to setup your backend.
Thanks.
Hi i have downloaded the source code nd everything was working fine until i entered the username and password details. It says signing failed, does the following code have to be pasted in php:
Also how would i link the iOS app to my phpAdmin
thank you
I believe you are missing the quotes around the query.
$query = “SELECT * FROM User WHERE username = ‘$username’ AND Password = ‘$password'”;
HI it says there is a t-string syntax error on line 14 … $query = SELECT * FROM User WHERE username = ‘$username’ AND Password = ‘$password’; i am really stuck
—CODE PHP FILE BELOW—
//Retrieve the login details via POST
$username = $_POST[‘username’];
$password = $_POST[‘password’];
//Query the table
$query = SELECT * FROM User WHERE username = ‘$username’ AND Password = ‘$password’;
//check if results are returned
$num = mysql_num_rows($query);
//if a record found matching query
if ($num == 1)
{
echo ‘{“success”:1}’;
}
else
{
echo ‘{“success”:0,”error_message”:”Username and/or password is invalid.”}’;
}
mysql_close();
?>
Can you look at the logs and see whats happening?
Does it post the request to the server?
Hi,
I am being sent to the Home view even if login is incorrect. I tried dragging the segue from the whole view. but I still face the same issue.
Whats the issue?
I dipin having issue with my program when I log in it it goes back to xcode then shows me an issue.
Hey,
checkout this post. https://dipinkrishna.com/blog/2014/07/login-signup-screen-tutorial-xcode-6-swift-ios-8-json/
i have attached the php part you need.
Hi Dipin, great tutorial!
I’m new to iOS development, and I’m trying to get the login informations from a database, so how can I do it using PHP?
Thats a permission issue at your server.
Great video but my question is when I replace the link with my own website with the same file I get an Access denied error how can i fix that?
Thanks so much for this tutorial, been looking everywhere for something like this!
Hey,
I have attached the “mysql” code to th end of the swift tutorial
Hi
My app will be use by more then one person, how do I get the php file to allow more usernames and passwords.
Thanks
Dear Dipin,
I am new to iOS development, i am trying your tutorial everything is great except one small issue.
I don’t have a server, i do not have the knowledge of PHP. So i got help from one of my friend and he designed a API for me and the link is http://localhost/MobileApp/Service1.asmx/Login?UserName=Parameter&Password=Parameter
how could i use this URL in this tutorial.
Please help me out.
Thanks
Thanks Dipin Krishna,
This is just what I needed to know to begin my project. I am a PHP developer and coming to Objective-C is very odd and hard to get to grips with.
Thanks, much appreciated! 🙂
🙂
Dont know how I missed that. Thank you Dipin, awesome tut and working like a champ.
The error hits because, You haven’t added the segue between login screen and the home screen.
Just figured it out and was being a idiot, this was my problem. I assigned Post = username and not username = POST. Fixed it now and this is the final PHP code to do validate checking on a database.
//Retrieve the login details via POST
$username = $_POST[‘username’];
$password = $_POST[‘password’];
//Query the table
$query = mysql_query(“SELECT * FROM CFP WHERE Email = ‘$username’ AND Password = ‘$password'”);
//check if results are returned
$num = mysql_num_rows($query);
//if a record found matching query
if ($num == 1)
{
echo ‘{“success”:1}’;
}
else
{
echo ‘{“success”:0,”error_message”:”Username and/or password is invalid.”}’;
}
mysql_close();
I have now got it to login but when its correct details the application crashes with this error:
2014-09-04 19:38:26.728 CrossfitPotch[41414:60b] PostData: username=j@cfp.com&password=admin
2014-09-04 19:38:26.757 CrossfitPotch[41414:60b] Response code: 200
2014-09-04 19:38:26.757 CrossfitPotch[41414:60b] Response ==> {“success”:1}
2014-09-04 19:38:26.757 CrossfitPotch[41414:60b] Success: 1
2014-09-04 19:38:26.758 CrossfitPotch[41414:60b] Login SUCCESS
2014-09-04 19:38:26.760 CrossfitPotch[41414:60b] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Receiver () has no segue with identifier ‘login_success”
*** First throw call stack:
(
0 CoreFoundation 0x017f31e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x015728e5 objc_exception_throw + 44
2 UIKit 0x0035348c -[UIViewController shouldPerformSegueWithIdentifier:sender:] + 0
3 CrossfitPotch 0x0000356b -[LoginViewController signinClicked:] + 3051
4 libobjc.A.dylib 0x01584880 -[NSObject performSelector:withObject:withObject:] + 77
5 UIKit 0x002343b9 -[UIApplication sendAction:to:from:forEvent:] + 108
6 UIKit 0x00234345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
7 UIKit 0x00335bd1 -[UIControl sendAction:to:forEvent:] + 66
8 UIKit 0x00335fc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
9 UIKit 0x00335243 -[UIControl touchesEnded:withEvent:] + 641
10 UIKit 0x00273ddd -[UIWindow _sendTouchesForEvent:] + 852
11 UIKit 0x002749d1 -[UIWindow sendEvent:] + 1117
12 UIKit 0x002465f2 -[UIApplication sendEvent:] + 242
13 UIKit 0x00230353 _UIApplicationHandleEventQueue + 11455
14 CoreFoundation 0x0177c77f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
15 CoreFoundation 0x0177c10b __CFRunLoopDoSources0 + 235
16 CoreFoundation 0x017991ae __CFRunLoopRun + 910
17 CoreFoundation 0x017989d3 CFRunLoopRunSpecific + 467
18 CoreFoundation 0x017987eb CFRunLoopRunInMode + 123
19 GraphicsServices 0x037e75ee GSEventRunModal + 192
20 GraphicsServices 0x037e742b GSEventRun + 104
21 UIKit 0x00232f9b UIApplicationMain + 1225
22 CrossfitPotch 0x0000277d main + 141
23 libdyld.dylib 0x01e3a701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Paste your php code.
This is the code from the debug console.
2014-09-04 19:25:00.449 CrossfitPotch[40463:60b] PostData: username=j@cfp.com&password=admin
2014-09-04 19:25:00.480 CrossfitPotch[40463:60b] Response code: 200
2014-09-04 19:25:00.481 CrossfitPotch[40463:60b] Response ==>
Notice: Undefined variable: POST in /Applications/XAMPP/xamppfiles/htdocs/Justin_Test/LoginTest.php on line 22
Notice: Undefined variable: username in /Applications/XAMPP/xamppfiles/htdocs/Justin_Test/LoginTest.php on line 26
{“success”:0,”error_message”:”Username and/or password is invalid.”}
2014-09-04 19:25:00.481 CrossfitPotch[40463:60b] Success: 0
If i hard code the PHP file and set the post varibles to the user and pass then it does yes, else if i leave it as it i get a Unidentified index error if i try open the PHP, which is likely due to the varibles having no set values yet from the program.
Does the php return “success:1” ??
Need a bit of assistance, im linking this to a mySQL database and have everything up and running fine on the xcode side but my php file wont seem to work with some modifying.
//MY PHP FILE CODE
$host = “localhost”; //Your database host server
$db = “CrossfitPotch”; //Your database name
$user = “root”; //Your database user
$pass = “root”; //Your password
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
die(“Database server connection failed.”);
}
else
{
//Attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//Retrieve the login details via POST
$_POST[‘username’] == ‘j@cfp.com’;
$password = $POST[‘password’];
//Query the table
$query = mysql_query(“SELECT * FROM CFP WHERE Email = ‘$username’ AND Password = ‘$password'”);
//check if results are returned
$num = mysql_num_rows($query);
//if a record found matching query
if ($num == 1)
{
echo ‘{“success”:1}’;
}
else
{
echo ‘{“success”:0,”error_message”:”Username and/or password is invalid.”}’;
}
mysql_close();
}
No matter what details I type back it will never carry onto the next story. I am trying to do this due to the fact that I have multiple users Id like to make in the end product.
You can use NSUserDefaults.
Saving:
Retrieving:
Found the problem. It was the URL. I have a 301 redirection for www on the link and it was not working becuase of that. I’ve add www on the url and now is working.
Can you please tell me how to push data from this request back to home screen?