Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Self-Destructing Message iPhone App Using Parse.com as a Backend and Adding Users Logging In and Logging Out

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Logging in and logging out. Part 2/4

I completely bamboozled over this one, I searched the forums an I couldn't find a helpful post. Best to all!

MainViewController.m
#import "MainViewController.h"
#import "LoginViewController.h"
#import <Parse/Parse.h>

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    PFUser *currentUser;
     PFUser *currentUser = [PFUsr currentUser];
    if (currentUser == nil) {

    [self performSegueWithIdentifier:@"showLogin" sender:self];
}

- (IBAction)logOut:(id)sender {

    [self performSegueWithIdentifier:@"showLogin" sender:self];
}

@end
LoginViewController.m
#import "LoginViewController.h"
#import "UIAlertView.h"
#import <Parse/Parse.h>

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)login:(id)sender {
    // This is an example of how to declare a block named loginHandler. It starts with a
    // return type, then the name of the block with a special syntax for naming. The next
    // set of parentheses hold the parameters and their types. Then assigning the block 
    // on the right side looks just like how we defined it in the video (carat with the
    // same parameters in parentheses).
    void (^loginHandler)(PFUser *user, NSError *error) = ^(PFUser *user, NSError *error) {
        if (error) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!"
                                                                message:@"Error logging in."
                                                               delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
        }
        else {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    };

    // Add your code below!

}

@end

3 Answers

And you've declared the PFUser instance twice:

    PFUser *currentUser;
     PFUser *currentUser = [PFUser currentUser];

Needs to be:

    PFUser *currentUser = [PFUser currentUser];

Or over two lines like:

    PFUser *currentUser;
    currentUser = [PFUser currentUser];

Typo here -> PFUser *currentUser = [PFUsr currentUser];

PFUsr not PFUser ... does that fix it?

Steve.

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Hi Steve,

In the code challenge " PFUser *currentUser;" is already there, the instructions are: "In viewDidLoad we see that the LoginViewController is presented automatically. Set the 'currentUser' variable using the appropriate method from the PFUser class. Then use that variable in an if statement to only perform the segue to LoginViewController if no user is logged in."

I tried the following code:

currentUser = [PFUser currentUser];
    if (currentUser) {

And I got this error: "Bummer! Don't forget to prevent performSegueWithIdentifier from being called! Hint: Check if 'currentUser' is nil."

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

I got it, my code looks like this:

currentUser = [PFUser currentUser];
    if (currentUser == nil) {