Add SelectUserViewController with basic sign-in/-out functionality

pull/3109/head
Jorge Canizales 10 years ago
parent 473d4fc3b9
commit 4da1012dc1
  1. 12
      objective-c/auth_sample/AuthSample.xcodeproj/project.pbxproj
  2. 24
      objective-c/auth_sample/Misc/AppDelegate.m
  3. 70
      objective-c/auth_sample/Misc/Base.lproj/Main.storyboard
  4. 8
      objective-c/auth_sample/SelectUserViewController.h
  5. 39
      objective-c/auth_sample/SelectUserViewController.m

@ -9,7 +9,7 @@
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
63E1E9821B28CB2100EF0978 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E1E9811B28CB2100EF0978 /* main.m */; }; 63E1E9821B28CB2100EF0978 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E1E9811B28CB2100EF0978 /* main.m */; };
63E1E9851B28CB2100EF0978 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E1E9841B28CB2100EF0978 /* AppDelegate.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 */; }; 63E1E98B1B28CB2100EF0978 /* SecondViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E1E98A1B28CB2100EF0978 /* SecondViewController.m */; };
63E1E98E1B28CB2100EF0978 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 63E1E98C1B28CB2100EF0978 /* Main.storyboard */; }; 63E1E98E1B28CB2100EF0978 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 63E1E98C1B28CB2100EF0978 /* Main.storyboard */; };
63E1E9901B28CB2100EF0978 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 63E1E98F1B28CB2100EF0978 /* Images.xcassets */; }; 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 = "<group>"; }; 63E1E9811B28CB2100EF0978 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
63E1E9831B28CB2100EF0978 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; }; 63E1E9831B28CB2100EF0978 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
63E1E9841B28CB2100EF0978 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; }; 63E1E9841B28CB2100EF0978 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
63E1E9861B28CB2100EF0978 /* FirstViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FirstViewController.h; sourceTree = "<group>"; }; 63E1E9861B28CB2100EF0978 /* SelectUserViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SelectUserViewController.h; sourceTree = "<group>"; };
63E1E9871B28CB2100EF0978 /* FirstViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FirstViewController.m; sourceTree = "<group>"; }; 63E1E9871B28CB2100EF0978 /* SelectUserViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SelectUserViewController.m; sourceTree = "<group>"; };
63E1E9891B28CB2100EF0978 /* SecondViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SecondViewController.h; sourceTree = "<group>"; }; 63E1E9891B28CB2100EF0978 /* SecondViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SecondViewController.h; sourceTree = "<group>"; };
63E1E98A1B28CB2100EF0978 /* SecondViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SecondViewController.m; sourceTree = "<group>"; }; 63E1E98A1B28CB2100EF0978 /* SecondViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SecondViewController.m; sourceTree = "<group>"; };
63E1E98D1B28CB2100EF0978 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; }; 63E1E98D1B28CB2100EF0978 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
@ -85,8 +85,8 @@
63E1E97E1B28CB2100EF0978 /* AuthSample */ = { 63E1E97E1B28CB2100EF0978 /* AuthSample */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
63E1E9861B28CB2100EF0978 /* FirstViewController.h */, 63E1E9861B28CB2100EF0978 /* SelectUserViewController.h */,
63E1E9871B28CB2100EF0978 /* FirstViewController.m */, 63E1E9871B28CB2100EF0978 /* SelectUserViewController.m */,
63E1E9891B28CB2100EF0978 /* SecondViewController.h */, 63E1E9891B28CB2100EF0978 /* SecondViewController.h */,
63E1E98A1B28CB2100EF0978 /* SecondViewController.m */, 63E1E98A1B28CB2100EF0978 /* SecondViewController.m */,
63F5DE471B28F5C100CDD07E /* GoogleService-Info.plist */, 63F5DE471B28F5C100CDD07E /* GoogleService-Info.plist */,
@ -216,7 +216,7 @@
files = ( files = (
63E1E98B1B28CB2100EF0978 /* SecondViewController.m in Sources */, 63E1E98B1B28CB2100EF0978 /* SecondViewController.m in Sources */,
63E1E9851B28CB2100EF0978 /* AppDelegate.m in Sources */, 63E1E9851B28CB2100EF0978 /* AppDelegate.m in Sources */,
63E1E9881B28CB2100EF0978 /* FirstViewController.m in Sources */, 63E1E9881B28CB2100EF0978 /* SelectUserViewController.m in Sources */,
63E1E9821B28CB2100EF0978 /* main.m in Sources */, 63E1E9821B28CB2100EF0978 /* main.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;

@ -33,5 +33,29 @@
#import "AppDelegate.h" #import "AppDelegate.h"
#import <Google/SignIn.h>
@implementation AppDelegate @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 @end

@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="49e-Tb-3d3"> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7702" systemVersion="14D131" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="49e-Tb-3d3">
<dependencies> <dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7701"/>
</dependencies> </dependencies>
<scenes> <scenes>
<!--First--> <!--Select User-->
<scene sceneID="hNz-n2-bh7"> <scene sceneID="hNz-n2-bh7">
<objects> <objects>
<viewController id="9pv-A4-QxB" customClass="FirstViewController" customModuleProvider="" sceneMemberID="viewController"> <viewController id="9pv-A4-QxB" userLabel="Select User" customClass="SelectUserViewController" sceneMemberID="viewController">
<layoutGuides> <layoutGuides>
<viewControllerLayoutGuide type="top" id="Ia1-K6-d13"/> <viewControllerLayoutGuide type="top" id="Ia1-K6-d13"/>
<viewControllerLayoutGuide type="bottom" id="4ug-Mw-9AY"/> <viewControllerLayoutGuide type="bottom" id="4ug-Mw-9AY"/>
@ -16,29 +16,75 @@
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/> <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews> <subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" text="First View" textAlignment="center" lineBreakMode="tailTruncation" minimumFontSize="10" translatesAutoresizingMaskIntoConstraints="NO" id="KQZ-1w-vlD"> <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" text="Please sign in." textAlignment="center" lineBreakMode="tailTruncation" minimumFontSize="10" translatesAutoresizingMaskIntoConstraints="NO" id="KQZ-1w-vlD" userLabel="Main Label">
<rect key="frame" x="221" y="279" width="157.5" height="41.5"/> <rect key="frame" x="222" y="190" width="157" height="28"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/> <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<fontDescription key="fontDescription" name="Helvetica" family="Helvetica" pointSize="36"/> <fontDescription key="fontDescription" name="Helvetica" family="Helvetica" pointSize="24"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/> <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/> <nil key="highlightedColor"/>
</label> </label>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Loaded by FirstViewController" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="A5M-7J-77L"> <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="A5M-7J-77L" userLabel="Sub Label">
<rect key="frame" x="203" y="329" width="194.5" height="17"/> <rect key="frame" x="301" y="226" width="0.0" height="0.0"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/> <fontDescription key="fontDescription" type="system" pointSize="14"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/> <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/> <nil key="highlightedColor"/>
</label> </label>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="W20-aa-6Xr">
<rect key="frame" x="150" y="370" width="300" height="50"/>
<subviews>
<view contentMode="center" translatesAutoresizingMaskIntoConstraints="NO" id="25e-pE-eb8" customClass="GIDSignInButton">
<rect key="frame" x="42" y="0.0" width="216" height="77"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="width" constant="216" placeholder="YES" id="Gfn-Hn-HXd"/>
<constraint firstAttribute="height" constant="77" placeholder="YES" id="cy1-xY-er0"/>
</constraints>
</view>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="RKb-EE-mCZ">
<rect key="frame" x="0.0" y="0.0" width="300" height="42"/>
<color key="backgroundColor" red="0.0" green="0.50196081399917603" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="width" constant="300" id="ILy-QC-YPq"/>
<constraint firstAttribute="height" constant="42" id="MFs-aL-fQF"/>
</constraints>
<fontDescription key="fontDescription" type="boldSystem" pointSize="14"/>
<color key="tintColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<state key="normal" title="Sign out">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="didTapSignOut" destination="9pv-A4-QxB" eventType="touchUpInside" id="lcC-7k-Rki"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="centerX" secondItem="25e-pE-eb8" secondAttribute="centerX" id="47f-A8-C2R"/>
<constraint firstAttribute="height" constant="50" id="EDJ-oo-Ix4"/>
<constraint firstItem="25e-pE-eb8" firstAttribute="top" secondItem="W20-aa-6Xr" secondAttribute="top" id="KSP-qb-mCJ"/>
<constraint firstItem="RKb-EE-mCZ" firstAttribute="top" secondItem="W20-aa-6Xr" secondAttribute="top" id="QDr-HV-DXb"/>
<constraint firstAttribute="width" constant="300" id="zWU-pZ-oQ7"/>
<constraint firstAttribute="centerX" secondItem="RKb-EE-mCZ" secondAttribute="centerX" id="zwy-cD-xao"/>
</constraints>
</view>
</subviews> </subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/> <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints> <constraints>
<constraint firstAttribute="centerX" secondItem="KQZ-1w-vlD" secondAttribute="centerX" id="6BV-lF-sBN"/> <constraint firstAttribute="centerX" secondItem="KQZ-1w-vlD" secondAttribute="centerX" id="6BV-lF-sBN"/>
<constraint firstItem="KQZ-1w-vlD" firstAttribute="top" secondItem="Ia1-K6-d13" secondAttribute="bottom" constant="170" id="82x-jY-eWa"/>
<constraint firstAttribute="centerX" secondItem="W20-aa-6Xr" secondAttribute="centerX" id="aHL-Ts-LoW"/>
<constraint firstItem="A5M-7J-77L" firstAttribute="top" secondItem="KQZ-1w-vlD" secondAttribute="bottom" constant="8" symbolic="YES" id="cfb-er-3JN"/> <constraint firstItem="A5M-7J-77L" firstAttribute="top" secondItem="KQZ-1w-vlD" secondAttribute="bottom" constant="8" symbolic="YES" id="cfb-er-3JN"/>
<constraint firstItem="A5M-7J-77L" firstAttribute="centerX" secondItem="KQZ-1w-vlD" secondAttribute="centerX" id="e1l-AV-tCB"/> <constraint firstItem="A5M-7J-77L" firstAttribute="centerX" secondItem="KQZ-1w-vlD" secondAttribute="centerX" id="e1l-AV-tCB"/>
<constraint firstAttribute="centerY" secondItem="KQZ-1w-vlD" secondAttribute="centerY" id="exm-UA-ej4"/> <constraint firstItem="W20-aa-6Xr" firstAttribute="top" secondItem="Ia1-K6-d13" secondAttribute="bottom" constant="350" id="pab-Li-xZ5"/>
</constraints> </constraints>
</view> </view>
<tabBarItem key="tabBarItem" title="First" image="first" id="acW-dT-cKf"/> <tabBarItem key="tabBarItem" title="Select User" image="first" id="acW-dT-cKf"/>
<connections>
<outlet property="mainLabel" destination="KQZ-1w-vlD" id="fWK-H9-WXN"/>
<outlet property="signInButton" destination="25e-pE-eb8" id="ovt-t5-7BV"/>
<outlet property="signOutButton" destination="RKb-EE-mCZ" id="afy-Q6-rK8"/>
<outlet property="subLabel" destination="A5M-7J-77L" id="DIj-ZX-1NK"/>
</connections>
</viewController> </viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="W5J-7L-Pyd" sceneMemberID="firstResponder"/> <placeholder placeholderIdentifier="IBFirstResponder" id="W5J-7L-Pyd" sceneMemberID="firstResponder"/>
</objects> </objects>
@ -47,7 +93,7 @@
<!--Second--> <!--Second-->
<scene sceneID="wg7-f3-ORb"> <scene sceneID="wg7-f3-ORb">
<objects> <objects>
<viewController id="8rJ-Kc-sve" customClass="SecondViewController" customModuleProvider="" sceneMemberID="viewController"> <viewController id="8rJ-Kc-sve" customClass="SecondViewController" sceneMemberID="viewController">
<layoutGuides> <layoutGuides>
<viewControllerLayoutGuide type="top" id="L7p-HK-0SC"/> <viewControllerLayoutGuide type="top" id="L7p-HK-0SC"/>
<viewControllerLayoutGuide type="bottom" id="Djb-ko-YwX"/> <viewControllerLayoutGuide type="bottom" id="Djb-ko-YwX"/>

@ -31,8 +31,12 @@
* *
*/ */
#import <Google/SignIn.h>
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController @interface SelectUserViewController : UIViewController <GIDSignInDelegate, GIDSignInUIDelegate>
@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 @end

@ -31,13 +31,46 @@
* *
*/ */
#import "FirstViewController.h" #import "SelectUserViewController.h"
@implementation FirstViewController @implementation SelectUserViewController
- (void)viewDidLoad { - (void)viewDidLoad {
[super 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 @end
Loading…
Cancel
Save