Google Sign in Integration into iOS App Clear Steps…
- Create A new Project with name GoogleSignInApp (Existed or Create depends on…) and close the project.
- Open terminal with (Command + Space) and type Terminal
- Terminal opened First Install Cocoa pods using Command $ sudo gem install cocoapods ( if already installed no need…)
- Then in Terminal Go to your project Directory by using the Command cd
5.Then in terminal pod init
6.open podfile
7. pod file created in project and a new window opened with the title pod file as shown below.
8.Add pod 'Google/SignIn' as shown below(note: use (Ctrl + “) to get straight quote like this ’ )
9. Then Save pod file by using (command + S) and close pod file using (command +W)
10. Now in already opened Terminal pod install as shown below.. pods installed successfully in our project…
11. Close terminal (command + W)
12. Now open Project and Check pods folder to see added pods or not..?
13. Open Workspace created in our project
14. once see this Link…
15. Then Click on GET A CONFIGURATION FILE or Click below Link..
16. Give App name (Its not necessary to give your app name…)
17. iOS Bundle ID (Which is available in our project)
18. Your country
19. Then CONTINUE TO Choose and configure services
20. Enable Google sign-In as shown below…
21. Then CONTINUE TO Generate configuration files
22. Download GoogleService-Info.plist (which downloaded into downloads)
23. Then Click Continue Adding Sign-In
24. Drag the GoogleService-Info.plist file you just downloaded into the root of your Xcode project
26. In the Project > Target > Info > URL Types panel, create a new item and paste your REVERSED_CLIENT_ID into the URL Schemes field. You can find your REVERSED_CLIENT_ID in the GoogleService-Info.plist file.
27. Also in the Project > Target > Info > URL Types panel, create a new item and type your bundle identifier in the URL Schemes field.
28. In your app delegate's .h file, declare that this class implements the GIDSignInDelegate protocol.
#import <Google/SignIn.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, GIDSignInDelegate>
29. In your app delegate's application:didFinishLaunchingWithOptions: method, configure the GGLContext shared instance and set the sign-in delegate.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSError* configureError;
[[GGLContext sharedInstance] configureWithError: &configureError];
NSAssert(!configureError, @"Error configuring Google services: %@", configureError);
[GIDSignIn sharedInstance].delegate = self;
return YES;
}
30. Implement the application:openURL:options: method of your app delegate. The method should call the handleURL method of the GIDSignIn instance, which will properly handle the URL that your application receives at the end of the authentication process.
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary *)options {
return [[GIDSignIn sharedInstance] handleURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}
For your app to run on iOS 8 and older, also implement the deprecated application:openURL:sourceApplication:annotation: method.
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[GIDSignIn sharedInstance] handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
31.In the app delegate, implement the GIDSignInDelegate protocol to handle the sign-in process by defining the following methods:
*** This Code is to get user Details from Google with Google Sign in****
- (void)signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error {
// Perform any operations on signed in user here.
NSString *userId = user.userID; // For client-side use only!
NSString *idToken = user.authentication.idToken; // Safe to send to the server
NSString *fullName = user.profile.name;
NSString *givenName = user.profile.givenName;
NSString *familyName = user.profile.familyName;
NSString *email = user.profile.email;
// ...
}
- (void)signIn:(GIDSignIn *)signIn
didDisconnectWithUser:(GIDGoogleUser *)user
withError:(NSError *)error {
// Perform any operations when the user disconnects from app here.
// ...
}
32. Next, you will add the Google Sign-In button so that the user can initiate the sign-in process. Make the following changes to the view controller that manages your app's sign-in screen: In the view controller's .h file, add the Google Sign-In interface and declare that this class implements the GIDSignInUIDelegate protocol.
#import <Google/SignIn.h>
@interface ViewController : UIViewController <GIDSignInUIDelegate>
- Add a GIDSignInButton to your storyboard, XIB file, or instantiate it programmatically. To add the button to your storyboard or XIB file, add a View and set its custom class to GIDSignInButton.
If you want to customize the button, do the following:
- In your view controller's .h file, declare the sign-in button as a property.
@property(weak, nonatomic) IBOutlet GIDSignInButton *signInButton;
- Connect the button to the signInButton property you just declared.
- Customize the button by setting the properties of the GIDSignInButton object.
**** in IBOutlet Write below Code****
[[GIDSignIn sharedInstance] signIn];
33.
You can use the signOut method of the GIDSignIn
object to sign out your user on the current device, for example:
- (IBAction)didTapSignOut:(id)sender {
[[GIDSignIn sharedInstance] signOut];
}