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

General Discussion

code challenge : logging in (Ribbit app) 2/4

Here's the link to the challenge: http://teamtreehouse.com/library/logging-in-and-logging-out. And here's my code:

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

- (void)viewDidLoad {
    [super viewDidLoad];
    PFUser *currentUser =[PFUser currentUser];
    if ([currentUser logOut]){
    [self performSegueWithIdentifier:@"showLogin" sender:self];};
};

- (IBAction)logOut:(id)sender {

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

@end

I have no idea what i m doing wrong and i don't really understand the question, can someone help me ?

3 Answers

Here is what I add

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

@interface LoginViewController ()

@end

@implementation LoginViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.hidesBackButton = YES;
}


- (IBAction)login:(id)sender {
    NSString *username = [self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *password = [self.passwordField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if ([username length] == 0 || [password length] == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops!"
                                                            message:@"Make sure you enter a username and password!"
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }
    else {

        [PFUser logInWithUsernameInBackground:username password:password block:^(PFUser *user, NSError *error) {
            if (error) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!"
                                                                    message:[error.userInfo objectForKey:@"error"]
                                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alertView show];
            }
            else {
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
        }];
    }
}

@end

Its been a few months, but I just tested the app and it works. Also make sure you add "showLogin" to the segue on the storyboard.

yes i would use this if i were building the app but this is for the code challenge question...so this doesn't really work, they expect a much simpler answer i think.....

Sorry try this:

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

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];


PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
    // do stuff with the user
} else {
    // show the signup or login screen
}
}



- (IBAction)logOut:(id)sender {

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

@end

I solved this, basically the answer lies around the condition being currentUser==nil but thanks !

The above code worked for me. Glad you figured it out : )

Can any of you help me out? Im stuck on this challenge