2017-07-28 56 views
1

我正尝试使用Google SignIn为使用Firebase的iOS应用验证用户身份。iOS:如果代理不是UIApplicationDelete,Google SignIn无法正常工作

在对GoogleSignInDependenies.framework文件进行黑客入侵以删除重复的类并根据Firebase和Google登录网站上的说明添加代码之后,我开始工作。其中一部分是将GIDSignInProtocol协议添加到应用程序委托并实现func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!)方法中的代码。

我现在想要设置另一个类作为登录的委托,但是当我重新指派委托时,回调不会发生,我不能为我的生活找到原因。

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate { 

    var window: UIWindow? 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     FirebaseApp.configure() 
     return true 
    } 

    public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) { 
     print("App delegate Signin") 
    } 
} 

class FirebaseSecurity:NSObject, GIDSignInDelegate { 

    override init() { 

     super.init() 
     GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID 

     // This works. 
//  GIDSignIn.sharedInstance().delegate = UIApplication.shared.delegate as! GIDSignInDelegate 

     // This doesn't work 
     GIDSignIn.sharedInstance().delegate = self 
    } 

    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) { 
     print("Security sign in") 
     } 
    } 
} 

轻弹两行安全init来回,我可以得到日志说:

App delegate Signin 

当我点击了GIDSignInButton我加入到我的UI,但没有当我切换作为代表FirebaseSecurity

任何人都知道为什么这不起作用?

+1

如果您在“FirebaseSecurity”对象中设置代理后立即“打印”某项内容,那么会在您点击“GIDSignInButton”之前或之后显示该行吗? –

+0

谢谢迈克尔,发现了这个问题,它就是我。请参阅下面的答案。 – drekka

回答

2

好吧,我的坏。当然,在花时间发布后,我几乎立即发现问题。

与Google登录无关。

我不小心将FirebaseSecurity设置为我的I容器中的一个瞬态实例,因此它在登录按钮被按下前被释放。卫生署!

即使一切正常,我仍会在此留下问题。也许别人会从我的错误中学到一些东西。 :-)