SelectUserViewController.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #import "SelectUserViewController.h"
  19. #import "MakeRPCViewController.h"
  20. @implementation SelectUserViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. self.signOutButton.layer.cornerRadius = 5;
  24. self.signOutButton.hidden = YES;
  25. // As instructed in https://developers.google.com/identity/sign-in/ios/sign-in
  26. GIDSignIn *signIn = GIDSignIn.sharedInstance;
  27. signIn.delegate = self;
  28. signIn.uiDelegate = self;
  29. // As instructed in https://developers.google.com/identity/sign-in/ios/additional-scopes
  30. if (![signIn.scopes containsObject:kTestScope]) {
  31. signIn.scopes = [signIn.scopes arrayByAddingObject:kTestScope];
  32. }
  33. [signIn signInSilently];
  34. }
  35. - (void)signIn:(GIDSignIn *)signIn
  36. didSignInForUser:(GIDGoogleUser *)user
  37. withError:(NSError *)error {
  38. if (error) {
  39. // The user probably cancelled the sign-in flow.
  40. return;
  41. }
  42. self.mainLabel.text = [NSString stringWithFormat:@"User: %@", user.profile.email];
  43. NSString *scopes = [user.accessibleScopes componentsJoinedByString:@", "];
  44. scopes = scopes.length ? scopes : @"(none)";
  45. self.subLabel.text = [NSString stringWithFormat:@"Scopes: %@", scopes];
  46. self.signInButton.hidden = YES;
  47. self.signOutButton.hidden = NO;
  48. }
  49. - (IBAction)didTapSignOut {
  50. [GIDSignIn.sharedInstance signOut];
  51. self.mainLabel.text = @"Please sign in.";
  52. self.subLabel.text = @"";
  53. self.signInButton.hidden = NO;
  54. self.signOutButton.hidden = YES;
  55. }
  56. @end