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

Using Parse.com - Logging In and Logging Out Code Challenge

Challenge Task 2 of 4

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.

My Code:

import "LoginViewController.h"

import "UIAlertView.h"

import <Parse/Parse.h>

@implementation LoginViewController

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

  • (IBAction)login:(id)sender {

    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!

PFUser *currentUser = [PFUser currentUser]; if (error) { [self performSegueWithIdentifier:@"showLogin" sender:self]; }

@end

Result:

Bummer! Compilation Error! Make sure you are setting 'currentUser' like in the video, and make sure you added the 'if' statement correctly.

1 Answer

You are writing a wrong if statement.

You need something like this:

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

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    PFUser *currentUser;
    currentUser = [PFUser currentUser];
    if (!currentUser) {
       [self performSegueWithIdentifier:@"showLogin" sender:self];
    }
}