gpt4 book ai didi

swift - Google 登录委托(delegate)没有被调用

转载 作者:行者123 更新时间:2023-11-30 10:35:42 24 4
gpt4 key购买 nike

我已将我的谷歌登录逻辑移至一个单独的类中,问题是,由于这样做,“sign”委托(delegate)函数不会被调用,我无法弄清楚为什么。

import Foundation
import Firebase
import GoogleSignIn

class Google: NSObject, GIDSignInDelegate {



func login(vc: UIView){

GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().presentingViewController = UIApplication.shared.keyWindow!.rootViewController!
GIDSignIn.sharedInstance().signIn()


}


func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {

print("Google Sign In didSignInForUser")
if let error = error {
print(error.localizedDescription)
return
}

guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: (authentication.idToken)!, accessToken: (authentication.accessToken)!)
// When user is signed in
Auth.auth().signIn(with: credential, completion: { (user, error) in
if let error = error {
print("Login error: \(error.localizedDescription)")
return
}
})
}
}

从 subview 调用 google 类:

import Foundation
import UIKit



class NewToMoonBar: UIView {


@IBOutlet var googleLogin: UIButton!
@IBOutlet var loginButton: UIButton!

static func show(vc: UIView) -> UIView {
let myCustomView = Bundle.main.loadNibNamed("NewToMoonBar", owner: self, options: nil)?.first as? NewToMoonBar
myCustomView!.frame.origin.x = 0
myCustomView!.frame.origin.y = 0
myCustomView?.frame.size.width = vc.frame.width
myCustomView?.frame.size.height = vc.frame.height
myCustomView!.tag = 100
return myCustomView!
}

@IBAction func loginButton(_ sender: Any) {

Facebook().login(vc: self)
}

@IBAction func googleLogin(_ sender: Any) {


Google().login(vc: self)
}


}

最佳答案

您已在此处创建了局部变量

 Facebook().login(vc: self)
Google().login(vc: self)

因此会发生取消分配并导致委托(delegate)不被触发,需要将它们设为实例变量

let f =  Facebook() 
let g = Google()

保留代表

关于swift - Google 登录委托(delegate)没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58082238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com