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
The log clearly shows the data send the json reply received.
The PHP code given shows that it looks for username ‘andrei’, but the username send is ‘username’.
2014-09-04 11:37:26.177 LoginScreenTutorial[3826:60b] PostData: username=username&password=password
2014-09-04 11:37:26.571 LoginScreenTutorial[3826:60b] Response code: 200
2014-09-04 11:37:26.571 LoginScreenTutorial[3826:60b] Response ==> {“success”:0,”error_message”:”Username and/or password is invalid. No post data!”}
2014-09-04 11:37:26.572 LoginScreenTutorial[3826:60b] Success: 0
and on PHP
header(‘Content-type: application/json’);
if(isset($_POST[‘username’]))
{
if($_POST[‘username’] == ‘andrei’ && $_POST[‘password’] == ‘password’)
{
echo ‘{“success”:1}’;
}
else
{
echo ‘{“success”:0,”error_message”:”Username and/or password is invalid.”}’;
}
}
else
{
echo ‘{“success”:0,”error_message”:”Username and/or password is invalid. No post data!”}’;
}
Can you paste the logs here?
For me is not working. It is not posting data. I’ve download the entire project from the link and is not posting data. Can you please help me?
If you are going to return something like this:
Then you can use the following code:
Great script. However I am going to be connecting to API that returns True false to the JSON request. I am trying to change code to reflect this. Any suggestions?
Any help would be amazing
Simon
Great stuff. Thanks for posting this. Keep up the good work!
Hey,
You will need to send a json reply from the server, and parse it in the app.
The tutorial does exactly that.
hye, i need you die and suggestion,
i have tried this tutorial to adapt my apps which to allow user to check their member card application status, sadly i can’t use it effectively.
i need an apps example that user will insert their application number thru mobile, and they will get the status of their application.
database mySQL. – storing users application status.
nice tutorial
Can you try this:
http://stackoverflow.com/questions/12540534/xcode-4-5-4g182-iphone-5-ios6-choose-a-destination-with-a-supported-archit
HI Dipin, amazing guide!
I’ve downloaded the code but when I try to simulate to any kind of device I got the error
xcode cannot run using the selected device
chose a destination with a supported architecture in order to run on this device.
My developer profile is expired but I don’t think it is a problem for running the app on simulator.
I’ve try looking at
Build Settings > Build Options > Compiler for C/C++/Objective-C (LLVM)
and
Bundle Name = ${PRODUCT_NAME}
Executable file = ${EXECUTABLE_NAME}
but still doesn’t work.
Any idea?
thank you
Matteo
Amazing tutorial thank you!
Hi,
Are you testing it on the simulator?
Can you confirm that you are connected to the internet?
Both of them result in a connection failed. I really need this to work, please help me!
Hm, lets try.
Can you check the response for a successful and an unsuccessful login, see if the page redirects for any one of them.
If the page ‘redirects’ to another page after successful authentication, then in the app we can check to see if the page is redirecting when trying to authenticate.
Hi Victor,
That method should be added to the “.m” file not “.h”.
Thanks.
Hello,
When I finished the application, xCode told me two errors :
1) http://hpics.li/873467e (Screenshot)
2) http://hpics.li/b5fb9da (Screenshot)
How can I fix this ?
Thank’s for this great tutorial!
No, I do not 🙁
Hey,
Do you have access to the website, to make changes?
I read all of your comments, and some had the same issue. Can you please tell me how to grab authentication code from a website and how to use them to check if I had correctly logged in? I know I’m asking for too much, but it would really help me if you replied!
Hi Dipin,
Love the tutorial very helpful but there’s one thing I need and it’s not working for me 🙁
I am developing an app for my school, where it uses a website with a login username and password. I was trying to make a login and username ViewController and take the user’s input and plug it in the HTML, and then opens a UIWebView and logs in with those credentials. Can you please help me? I need to submit this app to my school as soon as possible.
Thanks, it appears I was not connected. I just needed to let it connect, give it a few minutes and then start.
Hey,
Are you sure the device has an internet connection?
This tutorial works fine for the simulator, I can send data to and form the php page fine. But when I run it on my iPhone, it is unresponsive and then says “Login Failed! Connection Failed”.
In Xcode it says in the debugger the response code is 0 and “the request timed out.”. Why would the simulator be fine and the phone not? Please help!
Yes, will do one soon.
See the progress here : https://dipinkrishna.com/blog/2014/07/login-signup-screen-tutorial-xcode-6-swift-ios-8-json/
This is a very helpful tutorial. Do you plan on making one for xcode 6/ swift?
Yes. Using a DB will be the right choice.
Everything works great. The code was amazing and I thank you for that. I have only one question. How would a person use a “signup” button to save the information to the file? Or would it have to be to a “database” of some sort?
Thanks for this tutorial.
No Problem. Hope, you get it done.
Alright will try tomorrow but thank you very much you have helped me greatly
Copy over the authentication code from your actual login page, and see my sample php code to get an idea on what to return.
How would I go about that
🙂
The app is expecting a JSON response.
If possible, create a new url to process the requests from the app, and return a JSON response.
I’m not sure as I have only just stated to learn code and I am not experienced however I copied the code about json data and put in the URL and it sent me back to the same page so I assume so
Does that url return a JSON response?
Yeah the app use to crash on signing then I defined the signin correctly and now it comes up saying sign in failed but doesn’t crash the app and the URL is vle.biltonschool.co.uk
U mean, the app crashes on signin?
It’s the same error that James gets
What is the “Response” ?
I am trying to log on to my schools vle, I am entering my login and password which is correct however it won’t let me log in it comes up sign in failed
The server side code i used, is just a sample.
You will need to setup a database and get data from there.
Hi dipin, great tutorial two queries we are having our app developed
Firstly could our developers use your code for our one time login for users in our app.
Secondly if thousands of users were login too our app where would we access all there data from, will our developers need to link our app to our website and what security can be put in place to secure users details
Thanks
John
Ahh got it now i corrected the mistake and now it works good however when i enter the correct username and password it comes up sign in failed (i am trying to log onto my schools vle)
Hi,
Can you make sure ‘signinClicked’ is defined, if so, please paste here the code which is calling ‘signinClicked’.
If you had copied the code from tutorial, i think i have made a typo “sigininClicked”.
Thanks.
You can use NSUserDefaults.
Saving:
Retrieving:
This is an awesome tutorial! Is there a way to save username/password information so the user only has to login on first launch? Thanks!
hello, i have changed the website url to the one i want however when i enter the correct login username and password it crashes and says thread 1 signal SIGABRT then when i go to the debug area is says this:
2014-06-23 21:09:57.804 Bilton App[7370:60b] Cannot find executable for CFBundle 0x10943c030 (not loaded)
2014-06-23 21:10:07.033 Bilton App[7370:60b] -[KJViewController signinClicked:]: unrecognized selector sent to instance 0x109320400
2014-06-23 21:10:07.035 Bilton App[7370:60b] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[KJViewController signinClicked:]: unrecognized selector sent to instance 0x109320400’
*** First throw call stack:
(
0 CoreFoundation 0x000000010194a495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001016a999e objc_exception_throw + 43
2 CoreFoundation 0x00000001019db65d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010193bd8d ___forwarding___ + 973
4 CoreFoundation 0x000000010193b938 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000100257f06 -[UIApplication sendAction:to:from:forEvent:] + 80
6 UIKit 0x0000000100257eb4 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17
7 UIKit 0x0000000100334880 -[UIControl _sendActionsForEvents:withEvent:] + 203
8 UIKit 0x0000000100333dc0 -[UIControl touchesEnded:withEvent:] + 530
9 UIKit 0x000000010028ed05 -[UIWindow _sendTouchesForEvent:] + 701
10 UIKit 0x000000010028f6e4 -[UIWindow sendEvent:] + 925
11 UIKit 0x000000010026729a -[UIApplication sendEvent:] + 211
12 UIKit 0x0000000100254aed _UIApplicationHandleEventQueue + 9579
13 CoreFoundation 0x00000001018d9d21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x00000001018d95f2 __CFRunLoopDoSources0 + 242
15 CoreFoundation 0x00000001018f546f __CFRunLoopRun + 767
16 CoreFoundation 0x00000001018f4d83 CFRunLoopRunSpecific + 467
17 GraphicsServices 0x0000000103ac1f04 GSEventRunModal + 161
18 UIKit 0x0000000100256e33 UIApplicationMain + 1010
19 Bilton App 0x00000001000023e3 main + 115
20 libdyld.dylib 0x0000000101fe25fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
The app will work with any web service, as long as it returns the correct json response.