在 Google Sign In App Delegate (iOS / Swift) 中调用方法后关闭视图控制器



我已经在我的项目中实现了谷歌登录,在那里我有一个视图控制器,用于处理以模式呈现的登录。Google 登录按钮调用应用委托中的方法。

我希望能够在用户登录后关闭视图控制器,但我无法执行此操作。 图片在这里: https://i.stack.imgur.com/p6zRh.png

不能从应用委托调用 dismiss()。我尝试在我的登录视图控制器中放置一个函数来检查用户是否已登录,但这不会运行,因为登录过程是异步运行的(更不用说一个非常黑客的方法了)

//Code on the Login VC:
override func viewDidLoad() {
super.viewDidLoad()
//Google Sign in stuff
GIDSignIn.sharedInstance()?.presentingViewController = self
// Automatically sign in the user.
GIDSignIn.sharedInstance()?.restorePreviousSignIn()
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

GIDSignIn.sharedInstance().clientID = "my-client-id.apps.googleusercontent.com"
GIDSignIn.sharedInstance().delegate = self

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any]) -> Bool {
let googleDidHandle = GIDSignIn.sharedInstance().handle(url)
let facebookDidHandle = ApplicationDelegate.shared.application(app, open: url, options: options)
return facebookDidHandle || googleDidHandle
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error!) {
if let error = error {
if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
print("The user has not signed in before or they have since signed out.")
} else {
print("(error.localizedDescription)")
}
return
}
guard let idToken = user.authentication.idToken else { fatalError("couldn't get idToken")}// Safe to send to the server
googleSignIn(idToken: idToken)
}

用户登录后,我想关闭模式呈现的视图控制器,但我不知道如何从应用程序委托执行此操作。

您必须设置 uiDelegate 才能获取有关 Google 登录控制器存在或关闭的信息。

GIDSignIn.sharedInstance().uiDelegate = self

然后需要在课堂上实现GIDSignInUIDelegate

extension ViewController: GIDSignInUIDelegate {
//MARK: GoogleSignIn UI Delegate
public func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {
}
public func sign(_ signIn: GIDSignIn!,
present viewController: UIViewController!) {
}
public func sign(_ signIn: GIDSignIn!,
dismiss viewController: UIViewController!) {
dismiss(animated: true, completion: nil)
}
}

使用这些方法,您可以 关闭您的ViewController.

当用户点击"使用 Google 登录"按钮时。您可以调用以下方法。

@objc func didTapGooglePlusLoginButton(sender:UIButton)  {
GIDSignIn.sharedInstance().signIn()
}

您可以在登录 VC 中实现 GIDSignInDelegate,并在登录 VC 本身中添加 didSignInFor 委托方法,以便您可以处理输入和输出。

class LoginVC: UIViewController,GIDSignInDelegate 

您可以在以下函数中关闭或移动到另一个类

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if (error == nil) {
print(user.profile)
let userId = user.userID                  
self.Name = user.profile.name
self.givenName = user.profile.givenName
self.Email = user.profile.email
let navigationController =  UINavigationController(rootViewController: VC)
appDelegate?.window?.rootViewController = navigationController
// ...dissmiss or move to another controller
}
else {
print("(String(describing: error))")
}
}

感谢您提供的信息...你让我走上了正确的轨道!

这是我对最新(2019 年 8 月)Google 登录 Swift SDK 的解决方案,因为委托人姓名等有一些细微的变化。

所以基本上,将登录视图控制器设置为 GIDSignInDelegate:

class LoginController: UIViewController, GIDSignInDelegate {

将以下内容放在视图中DidLoad:

override func viewDidLoad() {
super.viewDidLoad()
//Google Sign in stuff
GIDSignIn.sharedInstance().clientID = "your-client-id.apps.googleusercontent.com"
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance()?.presentingViewController = self
// Automatically sign in the user.
GIDSignIn.sharedInstance()?.restorePreviousSignIn()

并在您的登录视图控制器中实现以下功能(根据谷歌文档)

@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
return GIDSignIn.sharedInstance().handle(url)
}
func application(_ application: UIApplication,
open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return GIDSignIn.sharedInstance().handle(url)
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error!) {
if let error = error {
if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
print("The user has not signed in before or they have since signed out.")
} else {
print("(error.localizedDescription)")
}
return
}
// 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
// ...
}
func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!,
withError error: Error!) {
// Perform any operations when the user disconnects from app here.
// ...
}

我认为您错过了在"appdelegate"文件中写入此方法。请检查以下方法。

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

有关更多谷歌登录信息,请查看以下链接:Github链接,如果它可以帮助您然后点击开始:)所以其他人会很容易找到它。

快乐的溺爱:)

最新更新