Last active
August 29, 2015 14:05
-
-
Save fisherds/0a090018b2e617840908 to your computer and use it in GitHub Desktop.
RHOAuthUtils methods to signIn and signOut of Endpoints OAuth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma mark - Sign in / sign out utils | |
| // Revoke the authorizer and clear the saved authorizer. | |
| + (void) signOut { | |
| [GTMOAuth2ViewControllerTouch removeAuthFromKeychainForName:kKeychainItemName]; | |
| [GTMOAuth2ViewControllerTouch revokeTokenForGoogleAuthentication:__authorizer]; | |
| [RHOAuthUtils _setAuthorizer:nil]; | |
| } | |
| + (void) signInFromViewController:(UIViewController*) parentViewController | |
| withCallback:(void (^)(NSError* error)) callback { | |
| __parentViewControllerForSignInModal = parentViewController; | |
| __signInCallback = callback; | |
| GTMOAuth2ViewControllerTouch* signInViewController; | |
| signInViewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kScope | |
| clientID:kIosClientID | |
| clientSecret:kIosClientSecret | |
| keychainItemName:kKeychainItemName | |
| delegate:self | |
| finishedSelector:@selector(_viewController:finishedWithAuth:error:)]; | |
| // Display the signInViewController within a Navigation Controller so we can add a cancel button in the top bar. | |
| UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:signInViewController]; | |
| navigationController.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] | |
| initWithTitle:@"Cancel" | |
| style:UIBarButtonItemStyleBordered | |
| target:self | |
| action:@selector(_cancelSignIn)]; | |
| navigationController.navigationBar.barStyle = UIBarStyleBlack; | |
| navigationController.navigationBar.translucent = NO; | |
| [parentViewController presentViewController:navigationController animated:YES completion:nil]; | |
| } | |
| # pragma mark - private methods | |
| // Called when a user presses Cancel from the Sign in modal. | |
| + (void) _cancelSignIn { | |
| [__parentViewControllerForSignInModal dismissViewControllerAnimated:YES completion:nil]; | |
| } | |
| // Called when a user completes Sign in. | |
| + (void) _viewController:(GTMOAuth2ViewControllerTouch*) viewController | |
| finishedWithAuth:(GTMOAuth2Authentication*) auth | |
| error:(NSError*) error { | |
| if (error != nil) { | |
| // Authentication failed. | |
| [RHOAuthUtils signOut]; | |
| } else { | |
| // Authentication succeeded. | |
| NSLog(@"Authentication success!"); | |
| [RHOAuthUtils _setAuthorizer:auth]; // Important!!! | |
| } | |
| if (![__parentViewControllerForSignInModal.presentedViewController isBeingDismissed]) { | |
| [__parentViewControllerForSignInModal dismissViewControllerAnimated:NO completion:nil]; | |
| } | |
| __signInCallback(error); | |
| __parentViewControllerForSignInModal = nil; | |
| __signInCallback = nil; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment