1

I need to open google login viewController as a modal from a existing viewController. That's ok but I don't know how to add a button for closing the modal because I'm not using an UINavigationController. Anybody having this same problem?

GTMOAuth2ViewControllerTouch *viewController;
        viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:GOOGLE_SCOPE_YOUTUBE
                                                                  clientID:GOOGLE_CLIENT_ID
                                                              clientSecret:GOOGLE_CLIENT_SECRET
                                                          keychainItemName:GOOGLE_KEYCHAIN
                                                                  delegate:self
                                                          finishedSelector:finishedSel];

        NSDictionary *params = [NSDictionary dictionaryWithObject:@"es" forKey:@"hl"];
        viewController.signIn.additionalAuthorizationParameters = params;
        viewController.signIn.shouldFetchGoogleUserProfile = YES;
        NSString *html = @"<html><body style=\"font-family:Arial\"><div style=\"text-align:center;\">Cargando página para iniciar sesión...</div></body></html>";
        viewController.initialHTMLString = html;
        [self presentViewController:viewController animated:NO completion:nil];

thanks

returnvoid
  • 434
  • 1
  • 7
  • 19

3 Answers3

1

I had the same problem and ended up using

[UIView  beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[self.navigationController pushViewController:detailView animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:NO];
[UIView commitAnimations];

But this is not the perfect solution (very ugly btw and non-flat). I am wondering why the Nav Bar doesn't show up for

[self presentViewController:viewController animated:NO completion:nil];

Yeah, we could add the Nav Bar manually

UINavigationBar *navBar=[etc…

I think perfect solution'd be to use the new UIViewTransitions (Apple wants to force us to use this?).

DeveloBär
  • 673
  • 8
  • 20
  • Could you tell me if finishedSelector is correctly called after the successfull login? – returnvoid Mar 21 '14 at 17:57
  • hmmm…I think that depends on the Google View Controller Code, not the ViewController itself. Have you added the delegate correctly? – DeveloBär Mar 22 '14 at 02:33
1

This is my re-interpretation of Imran Khan's excellent answer provided in his response to this stack overflow question: Google Drive iOS SDK: Display Cancel Login Button

Go there and up-vote his response, I have only copied and converted.

if (!self.isAuthorized) {
    SEL selectorFinish = @selector(viewController:finishedWithAuth:error:);
    SEL selectorButtonCancel = @selector(buttonCancelTapped:);

    UINavigationController *navController = [[UINavigationController alloc] init];

    UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 63)];
    UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:<<localised string for title>>];
    UIBarButtonItem *barButtonItemCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:selectorButtonCancel];

    [navigationItem setRightBarButtonItem:barButtonItemCancel];
    [navigationBar setTranslucent:NO];
    [navigationBar setItems:[NSArray arrayWithObjects: navigationItem,nil]];

    [navController.view addSubview:navigationBar];

    GTMOAuth2ViewControllerTouch *authViewController = nil;
    authViewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
                                                                    clientID:kClientID
                                                                clientSecret:kClientSecret
                                                            keychainItemName:kKeychainItemName
                                                                    delegate:self
                                                            finishedSelector:selectorFinish];

    [navController addChildViewController:authViewController];

    [self.parentTVC presentViewController:navController animated:YES completion:nil];
}

For clarity, the buttonCancelTapped: method is as follows...

- (IBAction)buttonCancelTapped:(UIBarButtonItem *)sender {
    [self.parentTVC dismissViewControllerAnimated:YES completion:^(void){}];
}

For clarity, the variable parentTVC is a public property,

@property (nonatomic, strong) UITableViewController *parentTVC;

and is set using a custom init method, as follows...

- (id)initWithParentTVC:(UITableViewController *)tvc {
    self = [super init];
    [self setParentTVC:tvc];

    return self;
}

This custom init method is called from the parent view controller.

Community
  • 1
  • 1
andrewbuilder
  • 3,629
  • 2
  • 24
  • 46
1

Swift Version - For the new users

Extend the class GTMOAuth2ViewControllerTouch

extension GTMOAuth2ViewControllerTouch
{
    public override func viewDidLoad()
    {
        super.viewDidLoad()
        let cancelItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(self.cancelGdriveSignIn))
        self.navigationController?.navigationBar.topItem?.rightBarButtonItem = cancelItem
        self.navigationController?.navigationBar.topItem?.title = "Google Drive"
    }

    func cancelGdriveSignIn()
    {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}

AND add a navigation Controller before returning your AuthController

private func createAuthController() -> UIViewController {
    let scopeString = scopes.joinWithSeparator(" ")
    let controller = GTMOAuth2ViewControllerTouch(scope: scopeString, clientID: kClientID, clientSecret: nil, keychainItemName: kKeychainItemName, delegate: self, finishedSelector: #selector(ViewController.viewController(_:finishedWithAuth:error:)))
    let navController = UINavigationController(rootViewController: controller)
    return navController
}

Works like charm.

Amit Singh
  • 2,698
  • 21
  • 49