Sunday, December 1, 2013

How To Integrate PFLogInViewController And PFSignUpViewController in iOS App

Please follow the previous tutorial, How To Integrate Parse With iOS App to get started with Parse in your iOS App.

In this tutorial, we are going to edit the Appdelegate.h and .m files to launch PFLogInViewController And PFSignUpViewController. 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  {  

   [Parse setApplicationId:@"YOUR_PARSE_APP_ID" clientKey:@"YOUR_CLIENT_KEY"];  
   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  

   // Override point for customization after application launch.  
   if ([PFUser currentUser]) // if user logged in.  

   {  
    self.homeViewController= [[HomeViewController alloc]initWithNibName:@"HomeViewController" bundle:nil];  
    self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.homeViewController];  

   } else  
   {  
    self.logInViewController = [self getLoginViewController];  
    self.navigationController = [[UINavigationController alloc]initWithRootViewController:logInViewController];  
   }  

   self.window.rootViewController = self.navigationController;  
   [self.window makeKeyAndVisible];  

   return YES;  

  }   

Copy paste this method to Appdelegate.m
 - (PFLogInViewController *)getLoginViewController
 {
   PFLogInViewController *loginController= [[PFLogInViewController alloc] init];
   [loginController setDelegate:self];
   loginController.logInView.logo = nil;

   loginController.fields = PFLogInFieldsUsernameAndPassword
   | PFLogInFieldsLogInButton
   | PFLogInFieldsSignUpButton
   | PFLogInFieldsPasswordForgotten;

   self.signUpViewController = [[PFSignUpViewController alloc] init];
   [signUpViewController setDelegate:self];

   signUpViewController.fields = PFSignUpFieldsUsernameAndPassword
   | PFSignUpFieldsSignUpButton|PFSignUpFieldsDismissButton;

   signUpViewController.signUpView.logo = nil;
   [loginController setSignUpController:signUpViewController];
   return loginController;
 }


Login and SignUp screens are set. Now we should include delegates for Login and SignUp.

Sent to the delegate to determine whether the log in request should be submitted to the server.
 - (BOOL)logInViewController:(PFLogInViewController *)logInController shouldBeginLogInWithUsername:(NSString *)username password:(NSString *)password { 
   // Validate username and pasword field.
   if (username && password && username.length != 0 && password.length != 0) { 
     return YES; // Begin login process 
   } 

   [[[UIAlertView alloc] initWithTitle:@"Missing Information" 
                 message:@"Make sure you fill out all of the information!" 
                 delegate:nil 
            cancelButtonTitle:@"ok" 
            otherButtonTitles:nil] show]; 
   return NO; // Interrupt login process 
 } 

Sent to the delegate when a PFUser is logged in.
 - (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {

   NSLog(@"Login sucessfull !! and username is %@",user.username);
   self.homeViewController= [[HomeViewController alloc]initWithNibName:@"HomeViewController" bundle:nil]; 
   [self.navigationController pushViewController:self.homeViewController animated:YES];\
 }

Sent to the delegate when the log in attempt fails.
- (void)logInViewController:(PFLogInViewController *)logInController didFailToLogInWithError:(NSError *)error 
{
   NSLog(@"Failed to log in...");
}

Sent to the delegate when the log in screen is dismissed.
- (void)logInViewControllerDidCancelLogIn:(PFLogInViewController *)logInController 
{
   NSLog(@"Canceled log in...");
}

Sent to the delegate to determine whether the sign up request should be submitted to the server.
- (BOOL)signUpViewController:(PFSignUpViewController *)signUpController shouldBeginSignUp:(NSDictionary *)info { 
   BOOL informationComplete = YES; 
   //Validate all datafields.
   for (id key in info) { 
     NSString *field = [info objectForKey:key]; 
     if (!field || field.length == 0) { // check completion 
       informationComplete = NO; 
       break; 
     } 
   } 

   // Display an alert if validation failed
   if (!informationComplete) { 
     [[[UIAlertView alloc] initWithTitle:@"Missing Information" 
                   message:@"Make sure you fill out all of the information!" 
                   delegate:nil 
              cancelButtonTitle:@"ok" 
              otherButtonTitles:nil] show]; 
   } 
   return informationComplete; 
 } 

Sent to the delegate when a PFUser is signed up.
- (void)signUpViewController:(PFSignUpViewController *)signUpController didSignUpUser:(PFUser *)user { 
   // SignUp sucessfull, Now dismiss the PFSignUpViewController 
   [self.signUpViewController dismissViewControllerAnimated:YES completion:nil]; 
} 

Sent to the delegate when the sign up attempt fails.
- (void)signUpViewController:(PFSignUpViewController *)signUpController didFailToSignUpWithError:(NSError *)error { 
   NSLog(@"Failed to sign up..."); 
 } 

Sent to the delegate when the sign up screen is dismissed.
- (void)signUpViewControllerDidCancelSignUp:(PFSignUpViewController *)signUpController { 
   NSLog(@"User dismissed the signUpViewController"); 
 } 

Done :)

2 comments:

  1. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...

    Mobile App Development Company in Chennai
    Android app Development Company in Chennai
    ios app development Company in Chennai

    ReplyDelete
  2. It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving.. very specific nice content. And tell people specific ways to live their lives.Sometimes you just have to yell at people and give them a good shake to get your point across.
    Mobile App Development Company
    Android app Development Company
    ios app development Company
    Mobile App Development Companies

    ReplyDelete