Level: ★☆☆ tag: “iOS Access Google, Facebook login” “iOS Google login” “iOS Facebook login” author: WYW review: QiShare team


Introduction: The author shared the content related to accessing Google and Facebook login using Firebase in iOS access Google and Facebook Login (1). In this article, the author will share the content of accessing Google and Facebook login SDK directly.

As for the configuration needed to create applications and projects on Google and Facebook open platforms, THE author has explained in the last article iOS Access to Google and Facebook Login (1). This is not explained in this article.

IOS Login to Google and Facebook (1) Use Firebase to log in to Google and Facebook. The Google and Facebook login buttons in the previous article were wrapped in Firebase. If we want to customize the Google and Facebook buttons, we can call the login API when we click the custom button.

1. Access Google login SDK

The following author first placed Google, Facebook login effect diagram.

1. Google and Facebook login renderings

The following author shares the way, process and some API of integrating Google login SDK.

2. Integrate the Google login SDK

To integrate with the Google Login SDK, Google provides two methods: using Cocoapods and directly dragging and dropping relevant Framework and related resource files into the project.

2.1 Integrate the Google login SDK through Cocoapods

The Podfile reads as follows:

pod 'GoogleSignIn'.'~ > 5.0.2'
Copy the code
2.2 Download the Google Login Framework directly
  • Google Sign – In the SDK 5.0.2

When downloading Google login Framework directly, we need to link the required Framework, and do relevant Objc configuration, etc. See the Get the Google Sign-in SDK for iOS for more details about the process and the optional Google Login Framework

3. Access operations related to Google login

3.1 Initialize the Google SDK when the application starts
#import <Firebase.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
	[GIDSignIn sharedInstance].clientID = kGoogleClientID;
    // Other code...
    return YES;
}
Copy the code
3.2 Google login needs to be handled in the following proxy method
3.2.1 Proxy methods in the appdelegate. m file
- (BOOL)application:(nonnull UIApplication *)application
            openURL:(nonnull NSURL *)url
            options:(nonnull NSDictionary<NSString *, id> *)options {
    
    return [[GIDSignIn sharedInstance] handleURL:url];
}

/ / ios (4.2, 9.0)
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation {
    if ([url.absoluteString containsString:kGoogleClientID]) {
        return [[GIDSignIn sharedInstance] handleURL:url];
    }
    return NO;
}
Copy the code
The proxy method in 3.2.2 scenedelegate. m
- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts  API_AVAILABLE(ios(13.0)) {UIOpenURLContext *openURLContext = URLContexts.allObjects.firstObject;
    if ([openURLContext.URL.absoluteString containsString:kGoogleClientID) {
        [[GIDSignIn sharedInstance] handleURL:openURLContext.URL];
    }
}
Copy the code
3.3 Code related to Google login button and code related to handling Google login failure result
#import <GoogleSignIn/GoogleSignIn.h>


      

// Set the proxy
[GIDSignIn sharedInstance].delegate = self;
// Must be set otherwise Crash will occur
[GIDSignIn sharedInstance].presentingViewController = self;

// Click the custom Google login button to invoke the following API
[[GIDSignIn sharedInstance] signIn];



// Implement the proxy method
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
    if(! error) {NSLog(User ID: %@, user.userID);
    } else {
        NSLog(@ "% @", error.debugDescription); }}Copy the code

In the above code, when we click on the custom Button call [[GIDSignIn sharedInstance] signIn]; , the Google login process will be executed. The result of successful or unsuccessful login is – (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error Callback in the proxy method of.

3.5 Google exits the API
[[GIDSignIn sharedInstance] signOut];
Copy the code

Below, THE author shares the way, process, part of API and the problem of switching Facebook account when accessing Facebook to log in to SDK.

2. Log in to the SDK through Facebook

1. Integrate the Facebook login SDK

Facebook Open Platform provides 4 ways, Swift Package Manager, Cocoapods, Carthage, direct download Facebook login SDK. Below I briefly explain the integration method of Cocoapods and directly download Facebook login SDK

If you want to use other integration methods, view Facebook Login for iOS – Quick Start

In Part 2, set the location of your development environment. Before logging in to Facebook iOS, set the integration option below your development environment. The integration instructions will appear in the Official Facebook documentation.

1.1 Integrate the Facebook login SDK through Cocoapods

The Podfile reads as follows:

pod 'FBSDKLoginKit'.'~ > 6.0.0'
Copy the code
1.2 Download Facebook login SDK directly

Download address: Facebook login SDK

2. Access Facebook login operations

2.1 When the application starts, call the code that prepares to use the Facebook SDK and initializes the Facebook SDK
 // To use the Facebook SDK, the following method should be called
[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
/ / register FacebookAppID
[FBSDKSettings setAppID:kFacebookAppID];
Copy the code
2.2 Facebook login needs to be handled in the following proxy method

The following proxy method is used to remove the authorization view from the previous mode when Facebook is installed on the phone, when jumping from our app to Facebook and then from Facebook back to our app.

2.2.1 Proxy methods in the appdelegate. m file
- (BOOL)application:(nonnull UIApplication *)application
            openURL:(nonnull NSURL *)url
            options:(nonnull NSDictionary<NSString *, id> *)options {
    
    if (@available(iOS 9.0, *)) {
        return [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
    } else {
        // Fallback on earlier versions}}/ / ios (4.2, 9.0)
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation {
    if ([url.absoluteString containsString:kFacebookAppID]) {
        return [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
    }
    return NO;
}
Copy the code
The proxy method in 2.2.2 scenedelegate. m
- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts  API_AVAILABLE(ios(13.0)) {UIOpenURLContext *openURLContext = URLContexts.allObjects.firstObject;
    if (openURLContext) {
        if ([openURLContext.URL.absoluteString containsString:kFacebookAppID]) {
             [[FBSDKApplicationDelegate sharedInstance] application:UIApplication.sharedApplication openURL:openURLContext.URL sourceApplication:openURLContext.options.sourceApplication annotation:openURLContext.options.annotation];
            return; }}}Copy the code
2.3 Code related to Facebook login button and code related to the result of successful and failed Facebook login
#import "FBSDKLoginKit.h"

// The Facebook login function can be invoked by calling the following code when clicking the Facebook login button
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];

[loginManager logInWithPermissions:@[@"public_profile"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult * _Nullable result, NSError * _Nullable error) {
    if (error) {
        NSLog(@"Process error");
    } else if (result.isCancelled) {
        NSLog(@"Cancelled");
    } else {
        NSLog(Token info: %@, result.token); }}];Copy the code

Logging out of Facebook invokes the following code. When encountering a Process Error, you can also try calling the following logging out API.

FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logOut];
Copy the code
2.4 Facebook Login Switching Problem

During the investigation, my colleague SH found the unfriendly problem of Facebook’s authorized login to switch accounts. Indeed, if Facebook is installed, users can switch Facebook accounts by changing the login account in the Facebook client. But users can’t switch accounts without a Facebook client installed. Because if you look closely at the renderings presented in the first part of this article, you can see that Facebook logins don’t offer a way to switch accounts like Google logins do.

LogOut ([[[FBSDKLoginManager alloc] init] logOut];) In the Facebook SDK roadmap, Revoke Permissions will be added to the roadmap. And the current Facebook login SDK does not support switching accounts. Can’t login with another account after logout

The current solution to switching Facebook accounts can be considered

For account switching, I later found that if the user did not have a Facebook client installed. If you want to change your Facebook account for authorized login, you can go to Settings -> Safari -> Advanced -> Site Data -> Upper right -> Edit -> Delete Facebook.com data.

After doing this, the User can use Facebook login authorization and the FacebookSDK will ask for the account password again.

If you encounter a Process Error reported by the FacebookSDK during this Process. FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init]; [loginManager logOut]; Call exit Facebook account to resolve the problem.

The following author shares the comparison of package volume, compilation time and existing project consideration between using Firebase to access Google and Facebook login and directly accessing Google and Facebook login SDK.

Third, compare Firebase access to Google and Facebook login and direct access to Google and Facebook login

Here’s my personal comparison of using Firebase to log in to Google (method 1) and Facebook (method 2) versus directly logging in to Google and Facebook (method 2).

1. Volume of the printed installation package

Podfile contents of method 1:

Pod 'Firebase/Auth', '~> 6.16.0' pod 'GoogleSignIn', '~> 5.0.2' pod 'FBSDKLoginKit', '~>6.0.0'Copy the code

Podfile contents for method 2:

Pod 'FBSDKLoginKit', '~>6.0.0' pod 'GoogleSignIn', '~> 5.0.2'Copy the code

The pod contents of modes 1 and 2 will have an additional Firebase/Auth

Method 1 compared to method 2. Method 1 requires some Google and Facebook code, images, localized strings and other resource files encapsulated by Pod Firebase. So in my current study, mode 1 Pods are about 4MB larger than mode 2 Pods. The author typed out the corresponding ipA package and compared it. The IPA package of mode 1 is about 1.1MB larger than that of mode 2.

Note: When I first started my research, the podfiles in mode 1 were loaded with more pod content, and the resulting IPA package in mode 1 was 7MB larger than in mode 2. So if you’re using Firebase to log in to Google or Facebook, be sure to look carefully at the documentation to see if you need a POD.

Method 1 also takes longer to compile than method 2.

2. Comparison of compilation time

Since I do not know how to measure compile time, I have only entered the following command in the terminal

defaults write com.apple.dt.Xcode ShowBuildOperationDuration YES

And see the project compile time at the top of Xcode.

The following figure shows the compilation time of mode 1 and mode 2 in the case of non-first compilation.

I have tried several times and found that method 1 always takes longer to compile than method 2, so if you want to reduce the compile time, you can consider logging into the SDK directly using Google and Facebook.

3. Consideration of existing projects

3.1 Using Firebase to Access Google and Facebook and Log in to the SDK

To log in to the SDK from Google and Facebook using Firebase, use Cocoapods for integration.

3.2 Directly Access Google and Facebook to log in to the SDK

Direct access to Google, Facebook login SDK integration more diversity. When logging in to the SDK directly from Google and Facebook, Google officially provides the integration method of Cocoapods and directly download the Framework. Facebook provides integration with Cocoapods, download Framework, Swift Package Manager, and Carthage.

4. Refer to the learning website

  • Google Sign-In for iOS
  • Facebook Login for iOS – Quick Start
  • Best iOS development tips and tricks

To learn more about iOS and related new technologies, please follow our official account:

You can add the following xiaobian wechat, and note to join the QiShare technical exchange group, xiaobian will invite you to join the QiShare technical Exchange Group.

QiShare(Simple book) QiShare(digging gold) QiShare(Zhihu) QiShare(GitHub) QiShare(CocoaChina) QiShare(StackOverflow) QiShare(wechat public account)

Recommended articles: 3D transformation in iOS (2) 3D transformation in iOS (1) WebSocket dual-end practice (iOS/ Golang) Today we are going to talk about WebSocket (iOS/Golang) using Swift for Bessel curve drawing Swift 5.1 (11) – method Swift 5.1 (10) – properties Strange dance group Android team – aTaller Strange dance weekly