diff --git a/objective-c/auth_sample/AuthSample.xcodeproj/project.pbxproj b/objective-c/auth_sample/AuthSample.xcodeproj/project.pbxproj index a3ea799..dd4778e 100644 --- a/objective-c/auth_sample/AuthSample.xcodeproj/project.pbxproj +++ b/objective-c/auth_sample/AuthSample.xcodeproj/project.pbxproj @@ -9,7 +9,7 @@ /* Begin PBXBuildFile section */ 63E1E9821B28CB2100EF0978 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E1E9811B28CB2100EF0978 /* main.m */; }; 63E1E9851B28CB2100EF0978 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E1E9841B28CB2100EF0978 /* AppDelegate.m */; }; - 63E1E9881B28CB2100EF0978 /* FirstViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E1E9871B28CB2100EF0978 /* FirstViewController.m */; }; + 63E1E9881B28CB2100EF0978 /* SelectUserViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E1E9871B28CB2100EF0978 /* SelectUserViewController.m */; }; 63E1E98B1B28CB2100EF0978 /* SecondViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E1E98A1B28CB2100EF0978 /* SecondViewController.m */; }; 63E1E98E1B28CB2100EF0978 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 63E1E98C1B28CB2100EF0978 /* Main.storyboard */; }; 63E1E9901B28CB2100EF0978 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 63E1E98F1B28CB2100EF0978 /* Images.xcassets */; }; @@ -23,8 +23,8 @@ 63E1E9811B28CB2100EF0978 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 63E1E9831B28CB2100EF0978 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 63E1E9841B28CB2100EF0978 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; - 63E1E9861B28CB2100EF0978 /* FirstViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FirstViewController.h; sourceTree = ""; }; - 63E1E9871B28CB2100EF0978 /* FirstViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FirstViewController.m; sourceTree = ""; }; + 63E1E9861B28CB2100EF0978 /* SelectUserViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SelectUserViewController.h; sourceTree = ""; }; + 63E1E9871B28CB2100EF0978 /* SelectUserViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SelectUserViewController.m; sourceTree = ""; }; 63E1E9891B28CB2100EF0978 /* SecondViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SecondViewController.h; sourceTree = ""; }; 63E1E98A1B28CB2100EF0978 /* SecondViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SecondViewController.m; sourceTree = ""; }; 63E1E98D1B28CB2100EF0978 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; @@ -85,8 +85,8 @@ 63E1E97E1B28CB2100EF0978 /* AuthSample */ = { isa = PBXGroup; children = ( - 63E1E9861B28CB2100EF0978 /* FirstViewController.h */, - 63E1E9871B28CB2100EF0978 /* FirstViewController.m */, + 63E1E9861B28CB2100EF0978 /* SelectUserViewController.h */, + 63E1E9871B28CB2100EF0978 /* SelectUserViewController.m */, 63E1E9891B28CB2100EF0978 /* SecondViewController.h */, 63E1E98A1B28CB2100EF0978 /* SecondViewController.m */, 63F5DE471B28F5C100CDD07E /* GoogleService-Info.plist */, @@ -216,7 +216,7 @@ files = ( 63E1E98B1B28CB2100EF0978 /* SecondViewController.m in Sources */, 63E1E9851B28CB2100EF0978 /* AppDelegate.m in Sources */, - 63E1E9881B28CB2100EF0978 /* FirstViewController.m in Sources */, + 63E1E9881B28CB2100EF0978 /* SelectUserViewController.m in Sources */, 63E1E9821B28CB2100EF0978 /* main.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/objective-c/auth_sample/Misc/AppDelegate.m b/objective-c/auth_sample/Misc/AppDelegate.m index a38e366..798d342 100644 --- a/objective-c/auth_sample/Misc/AppDelegate.m +++ b/objective-c/auth_sample/Misc/AppDelegate.m @@ -33,5 +33,29 @@ #import "AppDelegate.h" +#import + @implementation AppDelegate + +// As instructed in https://developers.google.com/identity/sign-in/ios/sign-in +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + NSError* configureError; + [GGLContext.sharedInstance configureWithError: &configureError]; + NSAssert(!configureError, @"Error configuring Google services: %@", configureError); + + return YES; +} + +// As instructed in https://developers.google.com/identity/sign-in/ios/sign-in +- (BOOL)application:(UIApplication *)application + openURL:(NSURL *)url + sourceApplication:(NSString *)sourceApplication + annotation:(id)annotation { + // This will properly handle the URL that the application receives at the end of the + // authentication process. + return [GIDSignIn.sharedInstance handleURL:url + sourceApplication:sourceApplication + annotation:annotation]; +} + @end diff --git a/objective-c/auth_sample/Misc/Base.lproj/Main.storyboard b/objective-c/auth_sample/Misc/Base.lproj/Main.storyboard index 5258761..761e7bf 100644 --- a/objective-c/auth_sample/Misc/Base.lproj/Main.storyboard +++ b/objective-c/auth_sample/Misc/Base.lproj/Main.storyboard @@ -1,13 +1,13 @@ - + - + - + - + @@ -16,29 +16,75 @@ - + + - + - + + + + + + + @@ -47,7 +93,7 @@ - + diff --git a/objective-c/auth_sample/FirstViewController.h b/objective-c/auth_sample/SelectUserViewController.h similarity index 81% rename from objective-c/auth_sample/FirstViewController.h rename to objective-c/auth_sample/SelectUserViewController.h index 9630f4d..eb3c2cf 100644 --- a/objective-c/auth_sample/FirstViewController.h +++ b/objective-c/auth_sample/SelectUserViewController.h @@ -31,8 +31,12 @@ * */ +#import #import -@interface FirstViewController : UIViewController - +@interface SelectUserViewController : UIViewController +@property(weak, nonatomic) IBOutlet GIDSignInButton *signInButton; +@property(weak, nonatomic) IBOutlet UIButton *signOutButton; +@property(weak, nonatomic) IBOutlet UILabel *mainLabel; +@property(weak, nonatomic) IBOutlet UILabel *subLabel; @end diff --git a/objective-c/auth_sample/FirstViewController.m b/objective-c/auth_sample/SelectUserViewController.m similarity index 58% rename from objective-c/auth_sample/FirstViewController.m rename to objective-c/auth_sample/SelectUserViewController.m index 1237c42..5e03056 100644 --- a/objective-c/auth_sample/FirstViewController.m +++ b/objective-c/auth_sample/SelectUserViewController.m @@ -31,13 +31,46 @@ * */ -#import "FirstViewController.h" +#import "SelectUserViewController.h" -@implementation FirstViewController +@implementation SelectUserViewController - (void)viewDidLoad { [super viewDidLoad]; - // Do any additional setup after loading the view, typically from a nib. + + self.signOutButton.layer.cornerRadius = 5; + self.signOutButton.hidden = YES; + + // As instructed in https://developers.google.com/identity/sign-in/ios/sign-in + GIDSignIn.sharedInstance.delegate = self; + GIDSignIn.sharedInstance.uiDelegate = self; + + [GIDSignIn.sharedInstance signInSilently]; +} + +- (void)signIn:(GIDSignIn *)signIn +didSignInForUser:(GIDGoogleUser *)user + withError:(NSError *)error { + if (error) { + // The user probably cancelled the sign-in flow. + return; + } + + self.mainLabel.text = [NSString stringWithFormat:@"User: %@", user.profile.email]; + NSString *scopes = [user.accessibleScopes componentsJoinedByString:@", "]; + scopes = scopes.length ? scopes : @"(none)"; + self.subLabel.text = [NSString stringWithFormat:@"Scopes: %@", scopes]; + + self.signInButton.hidden = YES; + self.signOutButton.hidden = NO; +} + +- (IBAction)didTapSignOut { + [GIDSignIn.sharedInstance signOut]; + self.mainLabel.text = @"Please sign in."; + self.subLabel.text = @""; + self.signInButton.hidden = NO; + self.signOutButton.hidden = YES; } @end