简体   繁体   中英

How do i move on to the next view controller after the user login?

I'm trying to use Parse in order to make users log in and sign up. However, I found an unexpected problem that blocks me from doing so.

If you use Parse, this is the sample code for PFUser and signup.

- (void)myMethod {
    PFUser *user = [PFUser user];
    user.username = @"my name";
    user.password = @"my pass";
    user.email = @"email@example.com";

    // other fields can be set just like with PFObject
    user[@"phone"] = @"415-392-0202";

    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            // Hooray! Let them use the app now.
        } else {
            NSString *errorString = [error userInfo][@"error"];
            // Show the errorString somewhere and let the user try again.
        }
    }];
}

and the problem is, I can't find a way to move on to the next page if the user managed to signup.

Parse's example projects are a great start. They suggest you show a View Controller first, let's call it 'Home'. Load it first regardless of the state a user is. Make it a delegate of the log in/ sign up views. Then in viewDidAppear tell signup/signin to present their views if there is no currentUser . So 'Home' will always load first, then after it's loaded into memory, viewDidAppear will call and act accordingly. See here: https://parse.com/tutorials/login-and-signup-views

HomeViewController.h

@interface HomeViewController : UIViewController <PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate>

HomeViewController.m

 - (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

  if (![PFUser currentUser]) { // No user logged in
    // Create the log in view controller
    PFLogInViewController *logInViewController = [[PFLogInViewController alloc] init];
    [logInViewController setDelegate:self]; // Set ourselves as the delegate

    // Create the sign up view controller
    PFSignUpViewController *signUpViewController = [[PFSignUpViewController alloc] init];
    [signUpViewController setDelegate:self]; // Set ourselves as the delegate

    // Assign our sign up controller to be displayed from the login controller
    [logInViewController setSignUpController:signUpViewController]; 

    // Present the log in view controller
    [self presentViewController:logInViewController animated:YES completion:NULL];
  } else {
      //They are already logged in so no reason to show any of the log in or sign up views. They will be able to use that app as normal
  }

}

Some great resources for beginners with Parse. I'd suggest bookmarking these as you should be using them often. Parse is fantastic at documentation and keeping them up-to-date, as well as just offering the variety of documentation they offer. They have all the answers you need, just have to look for them :):

Resources :

  1. Log In | Sign Up Views How-To [Obj C]
  2. Parse iOS SDK Reference
  3. Downloads & Changelogs
  4. iOS Developer Guide This one is important
  5. Parse Google Group

FYI, the log in views no longer look like the ones in the example or link. Although the code is the same, you should look at this blog to see it's new interface and protocols:

http://blog.parse.com/announcements/introducing-the-new-parseui-for-ios/

The documentations says - signUpInBackgroundWithBlock: signs up the user asynchronously. It is performing operation in background. Pushing a new ViewController onto navigation stack is an UI operation, so please make sure that you writing the code on the main thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM