Swift - How to implement a login/session (no code) - ios

My application has a login page and i would like to keep the user logged-in when he closes the app. Also, i have a logout button which should log-out the user and display the login page (even when he closes the app).
I tried to implement this using core data, but I have some issues with that and i'm not sure that is the best way to do it.
Can someone please give me some advices? I don't need code but just some idea of how i can implement that please.
Thanks!

You can make it as simple or complicated as you like. As simple as a Bool value in NSUserDefaults would do it. You could persist state in CoreData, Filesystem, NSUserDefaults. We store login credentials securely in the KeyChain.
For the authenticated areas of the app, check the state you have persisted on next app launch.
On logout, remove the state you have stored.

Related

How to keep the user logged in, in Swift (iOS)?

I am making an app that initially asks the user to log in, and on successful login.. takes the user to their dashboard. But, now, when I close the app.. I want the user to be logged in and the app to directly open from their dashboard, and not the login page. How do I do it?
Please see the link (gif), as I'm not able to show it here because it's my first question on stackoverflow
P.S. I'm new to iOS, I did surf internet and found something "UserDefaults".. but everything on it, included the use of navigation controller in the storyboard.. I'm not using any navigation controller.. but simply using a "show" segue for transition to the user dashboard.
My storyboard image, segue.
Please help!
Thanks.
The use of a navigation controller does not affect the user defaults. Anywhere in the app, you can call
let defaults = UserDefaults.standard
You can then write to defaults by calling
defaults.setValue(value, forKey: "key")
And later retrieve this value by doing
let savedValue = default.value(forKey: "key")
Depending on what you're using for login, you will need to decide what needs to be saved and retrieved, and where and when that needs to happen
I would agree with using UserDefaults, however, UserDefaults are easily accessible and are not safe. You would need to find a secure solution in case you are storing sensitive data (such as token). CoreData would be an option if you want to store data securely.
A third option would be to store the token in Keychain. This is also a secure method. The advantage is that the same token can be used by all of your apps (by the same developer) and the token persists even if the app is removed/re-installed.

iOS App, How to set it to one time registration?

I am Developing an App, that needs Registration, After launch Screen it shows Registration Screen. After Registration it allows to move on home Screen, and then any Task can be performed on the App.
Now I want my App to save the username and password into device, and on Launch, Application automatically registers the user.
if registration Successfully Achieved, then it automatically shows HomeScreen.
So my Question is, How do I achieve this?
I am beginner, Any Suggestion will be appreciated.
Thanks in Advance.
You should not to save password on your device. It's very not adviser to do that.
At the login, your API should return a session token. It is this one you have to save on device in Keychain. On launch, just retrieve the token in Keychain to be sure there is a session and go to HomeScreen, otherwise log out the user.
You can use this Lib https://github.com/matthewpalmer/Locksmith
You can use UserDefaults or Keychain.
https://developer.apple.com/reference/foundation/userdefaults
https://developer.apple.com/library/content/documentation/Security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html

Bypass login if user already created account

the reason I am asking this question is because I am not using firebase or parse to create accounts. I have used my own code to use CloudKit to login and so on. My question is, after the first time opening the app, and the user making an account, how can I make sure that the following time they open the app, it bypasses the login page? And then, if they logout, the next time they open the app, it takes them to the login page. How can this be done.
TL;DR how can I track whether or not a user has been logged in, and then open the app to a 2 possible scenes based on whether or not they are or are not logged in.
Firstly, you could keep the user sign in data (like a returned authkey, or a boolean flag) in the Defaults for the app.
Secondly, you could make a "Loading View" where you would check if that data is present.
Lastly, if the data isn't present move him to the login view, else to the main app.

Control flow for a webservice-backed iOS App

I'm learning the basics of iOS development, and I'd like to make a simple application that connects to a web service. I've got a lot of experience on the web application side, so I'm comfortable with what kinds of requests the app needs to send/receive etc. The part I'm not sure about is what the big picture architecture of a service-backed mobile application looks like.
When my application runs, I have one major requirement: the user must authenticate into the web service. The web service can send back a token and the app can use this for all subsequent requests. I want the user to be able to log in once, and for the app to stay logged in (ie the token remains valid for that device) indefinitely unless they log out.
Until the user logs in the application should really just be a login screen. If they log out, the same. Otherwise, they don't need to see the login screen at all.
So my question is, what is the right way to structure this?
In AppDelegate, do I want to make a LoginViewController and set it to the rootViewController? Then if the user is logged in, push to the main view for the rest of the app?
Or do I want to initialize the main part of the app (for instance, a UITabBarController with a few views in it), and check for a token, and then display a modal login screen if no token is available?
What I'm not clear on is what the rootViewController should be for an application like this, and how the app should keep track of whether the user is logged in, and determine what screen to show when the app is opened.
If anyone can give me a high level overview of how such an app should be structured, I'd really appreciate it.
Thanks!
There isn't really a right way to do this, either flow could be appropriate for an application. If I had some UI or data that would be displayed if a user is not logged in then I would use that as the initial rootViewController and use a modal login dialog to force the user to login. On the other hand, if I had nothing to display until a user has logged in then I would setup the login view controller to be the initial rootViewController if the user is not currently logged in.
For keeping track of the user being logged in you should leverage NSUserDefaults to persist the authentication token. Then in the application:didFinishLaunchingWithOptions: call to your app delegate look for this token in NSUserDefaults (and possibly validate it with the server) then set the rootViewController as appropriate.

iOS Twitter+OAuth check if user is logged in

I'm developing an app in which I want to give the user the option to be logged in with Twitter. My problem is that I want to check if the user is logged before given access to certain functions. Say I have a view and I want to show different content depending on the users is logged in or not. I know I can log the user in when opening the app, but I don't want to show the login screen every time if the user choose not to log in. (I'm using the Twitter+OAuth/MGTwitterEngine framework).
How can I set up a control like that? Any tips is appreciated!
Seems like you would want to save the authorization token in NSUserDefaults. Then, on launch, you would check for that token
if (![[NSUserDefaults standardUserDefaults] objectForKey:#"whatever_you_call_your_auth_token") {
//send the user to twitter login
} else {
//set isLoggedInViaTwitter:YES
}
So if you have a boolean value like isLoggedInViaTwitter, and you set that to YES or NO based on whether the auth token is present in NSUserDefaults, you can use the value of that to determine what content to present in your views.
I'm new but I hope this helps to some extent. If I've misunderstood your question, please let me know.

Resources