通过iOS中已安装的Gmail应用程序凭据登录Google帐户

时间:2018-11-20 17:57:20

标签: ios swift gmail google-signin

我的Google Sign In应用中有iOS Swift。它工作正常,但是每次我需要输入email idpassword时,它都可以正常工作。同时,与此同时,我在iPhone中使用Gmail App。如何在我的应用程序中使用该Gmail凭据并直接登录?是否可以使用Google-SignIn-iOS SDK

对于Google,FB,Outlook,Twitter,LinkedIn,我也需要同样的设置。

ViewController:

import GoogleSignIn

class ViewController: UIViewController, GIDSignInUIDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        GIDSignIn.sharedInstance().uiDelegate = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {

    }

    func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!) {


    }

    func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {



    }

}

AppDelegate:

import GoogleSignIn
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.


        GIDSignIn.sharedInstance().clientID = "3578***********************.apps.googleusercontent.com"
        GIDSignIn.sharedInstance().delegate = self

        return true
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url as URL?,
                                                 sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
                                                 annotation: options[UIApplicationOpenURLOptionsKey.annotation])
    }
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
              withError error: Error!) {
        if let error = error {
            print("\(error.localizedDescription)")
        } else {
            // Perform any operations on signed in user here.
            let userId = user.userID                  // For client-side use only!
            let idToken = user.authentication.idToken // Safe to send to the server
            let fullName = user.profile.name
            let givenName = user.profile.givenName
            let familyName = user.profile.familyName
            let email = user.profile.email
            // ...

            print("emailemailemail   ", email)
        }
    }

    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!,
              withError error: Error!) {
        // Perform any operations when the user disconnects from app here.
        // ...
    }
}

1 个答案:

答案 0 :(得分:1)

遵循这些步骤

第1步

在您的appdelegate中添加客户端ID,并进行深度链接,以在源应用程序中添加返回值

import GoogleSignIn

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        GIDSignIn.sharedInstance().clientID = "3578***********************.apps.googleusercontent.com"
        return true
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url as URL?,
                                                 sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
                                                 annotation: options[UIApplicationOpenURLOptionsKey.annotation])
    }

出于可重用性的目的,我为google signIn创建了通用类

import UIKit
import GoogleSignIn

class GoogleSDK: NSObject,GIDSignInDelegate,GIDSignInUIDelegate {
static let shared = GoogleSDK()
//MARK: Internal Properties
var signInBlock: ((GIDGoogleUser) -> Void)?

func googleSignIn()  {
    GIDSignIn.sharedInstance().delegate = self
    //        GIDSignIn.sharedInstance().signOut()
    //        GIDSignIn.sharedInstance().signIn()
    GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/plus.login")
    GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/plus.me")
    if GIDSignIn.sharedInstance().hasAuthInKeychain() == true {
        GIDSignIn.sharedInstance().signInSilently()
    } else {
        GIDSignIn.sharedInstance().signIn()
    }
}

//Google SignIn Delegates
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
    if error == nil {
        self.signInBlock?(user)

    } else {
        print("\(error.localizedDescription)")
    }
}

func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
    //  signInBlock(nil)
    //        if signInBlock != nil {
    //            signInBlock!(false, nil, error)
    //        }
}

}

在当前类上调用NSObject调用

 import GoogleSignIn

class ViewController: UIViewController, GIDSignInUIDelegate {

 // call the following method where you need
  // MARK: - Google Login
func GoogleLogin(){
    //  getTypeofLogin = getLoginType.Google.getName()
    GIDSignIn.sharedInstance().uiDelegate = self
    GoogleSDK.shared.googleSignIn()
    GoogleSDK.shared.signInBlock =   { (user) in
        if !user.userID.isEmpty{
            print(user)
            //self.handleSocialLogin(UID: user.userID, UName: user.profile.name, UEmail: user.profile.email, loginType:getLoginType.Google.getName())
        }
    }
}

}

要注销,请使用此

  GIDSignIn.sharedInstance().signOut()