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



我的iOS Swift应用程序中有Google Sign In。它工作得很好,但每次我都需要输入email idpassword。同时,我在iPhone中使用Gmail App。我如何使用我的应用程序中的Gmail凭据并直接登录?使用Google-SignIn-iOS SDK有可能吗?

我需要同样的谷歌,FB,Outlook,推特,领英。

视图控制器:

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

在您的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])
}

出于可重用性的目的,我为谷歌登录创建了公共类

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()

最新更新