iOS Twitter+OAuth check if user is logged in - ios

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.

Related

How to setup Change Password button for user?

I'm using Firebase Authentication in my iOS app, where the users signs up and logs in with an email and password.
If a user wants to change their password at some point, how can I set that up? I'm assuming that I need to connect an existing Firebase method to a UITextField. Any help/guidance is much appreciated!
Firebase Auth has a method called updatePassword (https://firebase.google.com/docs/auth/ios/manage-users#set_a_users_password):
Auth.auth().currentUser?.updatePassword(to: password) { (error) in
// ...
}
You'd probably want at least a UITextField (possibly two if you want to have them confirm the value), check that it meets any requirements you may have about being a certain length, including certain characters, etc (you probably have the same logic in your initial signup screen) and then a UIButton that triggers the updatePassword flow. Presumably since you've already set up your initial login you're comfortable adding these items to a screen and attaching actions to them.

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.

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.

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

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.

Few questions on iOS push notifications and logins

Here's my scenario. As part of my app when someone is sent a message it sends an alert to the phone. If they click on the alert I want to open up the specific message they were alerted to. I have a view message controller that will show the specific message. Here are my questions:
What is the best way to handle a notification while the app is open? I get the alert in the appDelegate, should I show an alert box that's triggered from there and open the correct controller if they choose to view it? This seems like code that doesn't belong in the appDelegate, but I don't know how that would otherwise happen.
For the login, this is a very similar question. When they are logged in it logs them into the server, and they stay logged in for a period of time. When the app loads I want to fire off a check on the server to see if their login has timed out. If it has I want to push them to the login screen of the app. Would this also happen in the app delegate?
My third question is how to best handle getting the phone id. I have the method set up in my app delegate where I get the ID when they accept the push notifications. My plan is to check and see if they are logged in, and if they are check to see if I already have their id saved to the server. If not send it up to save. Is that the best way to do this?
Yes, you would want to show a notification (UIAlertView is perfect for this), so the app doesn't suddenly change views, or jump around when a notification comes in. You'll want the user to be in control of whether they want to view the content related to the notification, just like they can choose to ignore notifications from apps anyway.
Yes, or switch the view to whatever view controller handled the login (you could do this modally). Be sure to let the user know why they're seeing the login view: "Login is required to view [NOTIFICATION]" or something like that. But it depends on the rest of the flow of your app.*
Not entirely sure which ID you're referring to? You might want to store a unique token in the app which you communicate to your server. This token is generated on the first login, for example which enables you to match up the user's login with the token. A UUID might work, or you can roll your own.
*A note on your auto-logout, why do you have this? Most apps stay logged in at all times, and let's the user control when they want to logout (Facebook, Twitter, Instagram, etc. - unless it's a banking app or PayPal). Alternatively, you can let the user add a custom four-digit login code like the Dropbox app for example.

Resources