Parse User Auto Login - ios

I'm trying to get my app to segue directly to the Profile Page after start is a PFUser is already logged in that way they won't have to keep logging in unless the completely stop the app. It's not working. Maybe I'm not checking for PFUser nil correctly. Please help. Here's what I'm putting in viewDidLoad.....
override func viewDidLoad()
{
super.viewDidLoad()
if PFUser.currentUser()?.username != nil
{
self.performSegueWithIdentifier("loginToProfileSegue", sender: self)
}
Please help.

Related

Displaying a different view when user is logged in swift not working

I am developing an app in Swift3, and using Parse.
For my start screen, I have a Sign In page. However, I want it to automatically go to the Home screen if the user is Logged In.
override func viewDidLoad() {
super.viewDidLoad()
if PFUser.current() != nil {
print((PFUser.current()?.username)!)
self.performSegue(withIdentifier: "ShowHomePage", sender: self)
}
else {
print("No User Logged in")
}
That's my code on the SignIn page. Im getting the print message showing me that a user is Logged In, however, it's not actually going to a different view.
Any help would be much appreciated.

The current user's settings are not being saved prior to the logout occurring

I was wondering if anyone could help me with this annoying bug I am having.
In my app, when the user logs out, I want to be able to change the user's property of "shareLocation" to false before that occurs.
However, it's not updating correctly.
I noticed however, when no PFUser.logOut() function call is made, the user's property will then update properly on the backend.
I also noticed that there are two different logout methods: logOutinBackground and logOutInBackgroundWithBlock
So I'm assuming that PFUser.logOut()'s execution happens before the save to background call?
Could someone help me find out the proper implementation of this?
Thank you in advance!
Relevant code snippets below. Please let me know if more info is needed.
PS - I also tried having the PFUser.logOut() come immediately after the user!.saveInBackground() call, in addition to the prepareForSegue method below, and the user was still not updating correctly prior to the logout and segue getting called.
#IBAction func btnLogout(sender: AnyObject) {
let user = PFUser.currentUser()
user!["shareLocation"] = false
user!.saveInBackground()
performSegueWithIdentifier("logoutSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "logoutSegue") {
PFUser.logOut()
}
}
You should save in background with a completion block, then, in the completion block trigger the logout request. In this way you know that the new status is saved and that you can safely logout. You can also check the status to be sure the save worked.
Try:
user!.saveInBackgroundWithBlock { (success: Bool!, error: NSError!) -> Void in
if success {
performSegueWithIdentifier("logoutSegue", sender: self)
} else {
NSLog("%#", error)
}
}
Because before the saveInBackground() is finished you're logging out the user.

Update cached copy of Parse currentUser() in Swift

I have an ios application, I have implemented Facebook login through Parse. Everything works great in the first trial.
I can login via Facebook
My details are stored in the PFUser.currentUser()
I am able to pull information from the Graph API
But things start to become a problem when I logout (PFUser.logOut()) , and delete the User row from the Parse class and delete the app from Facebook.
The following methods still show that the user is logged in and authenticated.
if let user = PFUser.currentUser() {
if user.isAuthenticated()
I am guessing it is because current user is still using the cached copy of the User. I try executing
do
{
try PFUser.currentUser().fetch()
}
catch{
print("User refreshed")
}
But it does not refresh the currentUser() and gives an error. How can I refresh the PFUser.currentUser(), so that it stops saying that the user is logged in and authenticated, even though it's not
Thanks
I got an answer here,
Swift & Parse - PFUser currentUser never equals nil
I've been struggling with logging out for a little while and I believe I have finally cracked it!
No matter what I did, when I used "PFUser.logOut()" would never set "PFUser.currentUser()" to nil, but it would set "PFUser.currentUser()!.username" to nil...
Because of this I used
var currentUser = PFUser.currentUser()!.username
as a global variable to track is a user is logged in.
On my login/first page I added
override func viewDidAppear(animated: Bool) {
if currentUser != nil {
self.performSegueWithIdentifier("login", sender: self)
}
}
and finally on my logout button i used
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "logout" {
PFUser.logOut() //Log user out
currentUser = PFUser.currentUser()!.username //Reset currentUser variable to nil
}
}

Performing automatic segue using PFUser.currentUser() on app launch

I have an app that is using Parse to grab the locally stored currently logged in user for my app. When I launch the app from the home screen I want to avoid forcing my users to login everytime so I grab the currently logged in user using PFUser.currentuser() like this:
override func viewDidLoad() {
super.viewDidLoad()
println(PFUser.currentUser())
if PFUser.currentUser()?.username != nil {
self.performSegueWithIdentifier("LoggedIn", sender: self)
}
}
I then just want to perform a segue to a "dashboard" if that user has already logged in. the code
self.performSegueWithIdentifier("LoggedIn", sender: self)
works beautifully everywhere else in my code. However, when I try to use it in ViewDidLoad() is when it is giving me trouble. Can anyone help me with this?
The solution is to perform the code snippet in my question in the ViewDidAppear() func like this:
override func viewDidAppear(animated: Bool) {
println(PFUser.currentUser())
if PFUser.currentUser()?.username != nil {
self.performSegueWithIdentifier("LoggedIn", sender: self)
}
}

Parse set applicationId quitting

I have an app that opens up right away to a login/register page. Everytime I run it, I get an error. The exception breakpoint shows that the error is in the viewDidLoadstatement. Here it is:
override func viewDidLoad() {
super.viewDidLoad()
Parse.setApplicationId("EVH469wqjtGjbWVATySdePvjaETquQDImcYaqUjc", clientKey: "4H9sMitCWq1AaaSvFSJ3EluUOwcI3OteYwMjrlxV")
var currentUser = PFUser.currentUser()
if currentUser != nil
{
self.performSegueWithIdentifier("homepage", sender: AnyObject?())
}
else
{
}
}
The breakpoint highlights the Parse.setApplicationId line. I cannot figure this out for the life of me. Any help is greatly appreciated!
UPDATE: It only crashes on my phone, not in the simulator... which is even weirder

Resources