10 January 2016

In working with the Google Drive SDK for iOS, one can appreciate its broad support for many different use cases including multiple iOS versions. Perhaps further updates are necessary as using the library in a Swift world requires significant effort.

After successfully integrating the SDK into a production app I can share some tips for making it a smoother experience.

For reference, the install guide by Google can be found here.


The OAuth2-based login screen had a UI problem where it wasn’t showing a cancel button thereby preventing cancellation of the login by users.

I’m listing some changes to GTMOAuth2ViewControllerTouch.m that will add a cancel button to the login screen.

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setUpNavigation];

    // ADD THIS CODE TO THIS METHOD:
    [self.navigationController.navigationBar setTitleTextAttributes:@{
        NSForegroundColorAttributeName : [UIColor blueColor]
    }];    
    self.navigationController.navigationBar.translucent = NO;

    CGFloat deviceWidth = [[UIScreen mainScreen] bounds].size.width;
    CGFloat navBarHeight = 64;
    UINavigationBar *navBar = 
        [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 
                                                          deviceWidth, navBarHeight)];
    [self.view addSubview:navBar];

    UIBarButtonItem *cancelItem = 
        [[UIBarButtonItem alloc] initWithTitle:[NSString 
                                 stringWithFormat:NSLocalizedString(@"Cancel", nil)]
                                 style:UIBarButtonItemStylePlain target:self
                                 action:@selector(cancelLogin:)];                                 
    UINavigationItem *navBarItem = 
        [[UINavigationItem alloc] initWithTitle:@"Google Drive"];
    navBarItem.rightBarButtonItem = cancelItem;
    navBar.items = [NSArray arrayWithObjects:navBarItem, nil];
}

// ADD THIS METHOD:
- (void)cancelLogin:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:^(void) {
    }];
}

- (void)setUpNavigation
{
    rightBarButtonItem_.customView = navButtonsView_;
    self.navigationItem.rightBarButtonItem = rightBarButtonItem_;
}

The final result.



blog comments powered by Disqus