I am a web developer that has recently moved over to iOS dev. I have implemented a login system for my website and I want to integrate this same functionality within my app. I will have login UI in the app that communicates with the web server using the AFNetworking library to make POST requests.
However, I am not sure how I will deal with remembering things in regard to user logins such as whether the user was logged in previously? With Web, I would do cookies and sessions, but I am not sure whether this is the best approach with iOS. I've also read about some people talking about storing things within the iOS keychain for security reasons. Can anyone shine some light on what the best practise for achieving what I want would be?
Thanks!
I would use NSUserDefaults but I think you should use token-based approach for your iOS client. When ever you log in and get a successful response, you should store your token in user defaults with the following code.
[[NSUserDefaults standardUserDefaults] setValue:token forKey:#"auth_token"];
[[NSUserDefaults standardUserDefaults] synchronize];
Your logout function could look something like this.
- (void)logout {
[[NSUserDefaults standardUserDefaults] setValue:nil forKey:#"auth_token"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
You can send this auth_token each time you make a web request so that the server knows those requests are authorized.
In objective-C there is the NSUserDefaults that can be used similarly.
To save a bool value for example you would do the following
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"IsLoggedIn"];
[[NSUserDefaults standardUserDefaults] synchronize];
to later load the value you write
BOOL isLoggedIn = [[NSUserDefaults standardUserDefaults] boolForKey:#"IsLoggedIn"];
Hope it helps!
Related
After successfully login with services call, I am showing dashboard into my application, there we have another service calls.
Problem is after running time application and then killing the app thread the link between app and services call is collapsed.
How to maintain that user is already logged if he/she login into application and kills the app. (Killing app double click home button and swipe up the app).
Do I have to call same login services on application did finished launch.
or check the user already login
My Question here is maintaining Session for accessing other services. But from server side its always says user is not login In after killing app from back ground.
#All
Your info helpful in make for integration services.
Thanks In Advance.
You can set a BOOL value to NSNumber object and add it to NSUserDefault when the login finish.
NSUserDefaults *boolUserDefaults = [NSUserDefaults standardUserDefaults];
[boolUserDefaults setObject:[NSNumber numberWithBool:YES]
forKey:#"loginStatus"];
Later you'll be able to retrieve that value as plain BOOL using -boolForKey: function in NSUserDefaults and based on that you can check if a user is already logged in.
Also you can maintain in Local database, coredata etc based on your requirement.
UPDATE
If you want to maintain the session, You can ask the server team to provide you an accesstoken on the login API response & save that as a String in NSUserDefaults and pass that string in your service calls.
If login is success, have below.
[[NSUserDefaults standardDefaults] setValue:#"yes" forKey:#"isLoggedIn"];
[[NSUserDefaults standardDefaults] synchronize];
For checking use below.
if ([[[NSUserDefaults standardDefaults] valueForKey:#"isLoggedIn"] isEqualToString:#"yes") {
NSLog(#"You are logged in");
} else {
NSLog(#"You are not logged in");
}
I am not sure about your server implementation. Usually, when logged in, the client(mobile app) will receive a token. This token should be included in the requests made afterward to show to the server that this user is logged in.
The http header is a good place to start to check whether some token is set when the login api is returned from the server. If yes, you may need to save that token:
[[NSUserDefaults standardDefaults] setValue:token forKey:#"loginToken"];
[[NSUserDefaults standardDefaults] synchronize];
Then, if the app is killed, upon launch:
NSString *token = [[NSUserDefaults standardDefaults] valueForKey:#"loginToken"];
//set the token to http header
I am developing an app that will eventually interact with a website through a web service that supports many users. The app user will then need a userid and password to synch up data. For an url connection to a server that requires authentication, answers on SO suggest that nsurl credentials or keychain are the way to go. However, initially, I just want to give the user and app a userid--that is persistent but can be changed when desired--so that I can do things like incorporate the userid in profile picture names and so forth following the format used in the web app.
Ideally, I'd like to do this now in a way that I don't have to repeat work later.
Is there a best practice for storing a user's userid in an app that I could do now prior to actually connecting to any web services?
Thanks for any suggestions.
You can use the NSUserDefaults to save persistent user data
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:#"12234" forKey:#"userID"];
[userDefaults synchronize];
You can then get the User ID back by using
NSString* userID = (NSString *) [userDefaults objectForKey:#"userID"];
Class Reference:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html
I was searching for a way to change the iOS device Locale without having to go through the settings and importing some new framework. Just a simple script or so would be great, or even a piece of code. I was wondering if there was something like :
[[NSLocale currentLocale] setLocale:#"WhateverLocale"];
This may or may not fulfill your needs, but one way to gain programmatic access over the app's current locale is to override the AppleLanguages key in NSUserDefaults. For example, to change to French:
[[NSUserDefaults standardUserDefaults] setObject:#[ #"fr" ]
forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
The downside to this approach is that you need to close and reopen the app for the change to take effect.
Note, to return to the system default, simply call:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"AppleLanguages"];
I have an iOS application that uses the keychain to store some information related to the authentication. I would like to remove this data from the keychain when the application is uninstalled.
How can I get aware of the application being uninstalled/removed?
You canĀ“t detect when your application is going to be deleted from your device. But you could detect when your app is launched for first time, after to be installed. For this I use the next code:
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunched"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];
//Here delete your info kept in your keychain. If not exist will not delete nothing, but if it exist mean your app has been installed again.
[removeYourPropertyInKeychain];
}
Is there a way to get the date when the user downloaded / installed the first time an app via Facebook ? In my app , in the first launch you have to connect with Facebook. So I wonder , is there a way to get the date of this first connection.
I found nothing interesting during my research on internet..
Thanks !
Here is multiple way to do this.
1.> You can store it on server end along with device UUID at login time.When you login first time with facebook via a remote connection , at server end you can put a check that this UUID is aready exist or not. If it is not then you can insert this UUID along with current date and time into server end database.
2.> You can store it locally in NSUserDefaults . Whenever you login with facebook , just check NSUserDefaults value via this code
if (![[NSUserDefaults standardUserDefaults] valueForKey:#"firstTimeFaceBookLogin"])
{
[[NSUserDefaults standardUserDefaults] setValue:[NSDate date] forKey:#"firstTimeFaceBookLogin"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
NSLog(#"current date ===%#",[[NSUserDefaults standardUserDefaults] valueForKey:#"firstTimeFaceBookLogin"]);
Now NSUserDefaults value any where in your app via firstTimeFaceBookLogin key. But if you delete your application from device then this NSUserDefaults would be clear and again create a store new value after installing it in your device.
I hope you understand it. Please let me inform if you still having any confusion. Thanks