0

Before I begin, I have searched Stackoverflow for how to do this, and I saw a lot of related posts, but none worked for me and I'm not sure why.

So basically I have a loginViewController, and in it, I have a method that call's GoogleSignIn:

- (void)googleTap:(id)sender
{
    [[GIDSignIn sharedInstance] signIn];
}

Now the way GoogleSignIn is set up, the result of that sign in call is handled inside AppDelegate.m

- (void)signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
     withError:(NSError *)error {
    // Perform any operations on signed in user here.
    if (!error) {
        NSString *userId = user.userID;                  // For client-side use only!
        NSString *idToken = user.authentication.idToken; // Safe to send to the server
        NSString *name = user.profile.name;
        NSString *email = user.profile.email;

        NSLog(@"Name: %@, User: %@, Token: %@, Email: %@",name, userId, idToken, email);

        // ...
    }
}

Inside this AppDelegate method, I want to call a method from my loginViewController:

-(void)onSuccessfulLogin{
    NSLog(@"On Successful Login");
    [self.navigationController pushViewController:[collectionViewController new] animated:YES];
}

I tried these answers: Calling UIViewController method from app delegate

want to call ViewController's method from appdelegate

and the NSLog is called, but the new ViewController is never pushed...why is that and how can I get that to work?

Community
  • 1
  • 1
stellarowl12
  • 525
  • 6
  • 18

3 Answers3

0

If this is your app delegate you have no self.navigationController. You probably changed the name of your NavigationController to something like UINavigationController *navigationController = [[UINavigationController alloc] init] You need to set a @property for a nav controller on the delegate class. Then where you initialize the nav controller

 UINavigationController *navigationController = [[UINavigationController alloc] init]`
self.navigationController = navigationController// You need this line.


//In your method
    [self.navigationController pushViewController:[collectionViewController new] animated:YES];
NSGangster
  • 2,397
  • 12
  • 22
  • Hey Matthew, thanks for your help. That method is actually in my loginViewController.m which definitely does have access to a self.navigationController. When I call that method from within the loginViewController.m, it works perfectly. Now I'm trying to call that same method from within AppDelegate. I need to do this because my Google SignIn has data received inside AppDelegate. – stellarowl12 Jan 29 '16 at 05:40
  • You should pass that data to your viewController or directly to your navigationController. When your referencing `self` inside the app delegate it means self is an instance of App Delegate, not the viewController. You need an instance of your viewController, which is a rootViewController of the navController, to call the pushViewController. Something that would look like `[loginViewControllerInstance.navigationController pushViewController:[collectionViewController new] animated:YES];` – NSGangster Jan 29 '16 at 15:09
0

Declare your onSuccessfulLogin method in YourViewController.h (header file)

In (void)signIn:didSignInForUser:withError: method, put below code at bottom

if ([self.navigationController.viewControllers.lastObject respondsToSelector:@selector(onSuccessfulLogin)]) {
    [((YourViewController *)self.navigationController.viewControllers.lastObject) onSuccessfulLogin];
}
Vũ Ngọc Giang
  • 234
  • 3
  • 12
0

I was thinking about this wrong all along. You can move the GDSignInDelegate from AppDelegate to viewcontroller.h.

Then you can simply move the -(void)signIn: didSignInUser: method to your ViewController. And you can call your method from there!

stellarowl12
  • 525
  • 6
  • 18
  • Right, doing it this way when referencing self your actually referencing an instance of ViewController so your `pushViewController` method will actually work :) – NSGangster Jan 29 '16 at 15:12