2017-01-09 84 views
1

我用火力地堡实现Gmail登录和我能够成功地让用户按下它具有连接作用GIDSignIn.sharedInstance().signIn()自定义按钮后,验证他的电子邮件。Gmail的认证不返回到应用

然而,一旦用户允许基本配置文件访问不会返回到应用程序,它只是这个屏幕:

login

是否有人知道如何使它回到应用程序后,用户认证?这是我的控制台:

017-01-09 09:47:46.565367 Dog_Log[3922:829530] [MC] Reading from private effective user settings. 
2017-01-09 09:47:53.702 Dog_Log[3922:] <FIRAnalytics/WARNING> Implementation of application:openURL:sourceApplication:annotation: not found. Please add the handler into your App Delegate. Class: Dog_Log.AppDelegate 

回答

5

问题:

警告告诉你的application(_:open:options:)实施无法找到:

2017-01-09 09:47:53.702 Dog_Log[3922:] <FIRAnalytics/WARNING> Implementation of application:openURL:sourceApplication:annotation: not found. Please add the handler into your App Delegate. Class: Dog_Log.AppDelegate 

解决方案:

你可能忘了application(_:open:options:)方法impl在它中调用handle(_:sourceApplication:annotation:),处理重定向到您的应用程序。你AppDelegate.swift应该像这样(提示是代码中的注释):

import UIKit 
import Firebase 
import GoogleSignIn 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

    // configure firebase 
    FIRApp.configure() 

    // configure google 
    GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()?.options.clientID 

    return true 
    } 

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { 

    // handled to go back to application after google log in 
    let handled = GIDSignIn.sharedInstance().handle(url, sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: [:]) 

    return handled 
    } 

    // other methods 
} 

例的ViewController您做出符合GIDSignInUIDelegate和GIDSignInDelegate,访问签到方法和一个自定义按钮的@IBAction使用:

import UIKit 
import Firebase 
import GoogleSignIn 


class ViewController: UIViewController, GIDSignInUIDelegate, GIDSignInDelegate { 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    // make the view controller the delegate 
    GIDSignIn.sharedInstance().uiDelegate = self 
    GIDSignIn.sharedInstance().delegate = self 
    } 

    // your action of the button to call the signIn method 
    @IBAction func pressGoogleSignInButton(_ sender: UIButton) { 
    GIDSignIn.sharedInstance().signIn() 
    } 

    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) { 
    if let error = error { 
     print("failed to log into google: ", error) 
     return 
    } 

    print("successfully logged into google ", user) 

    guard let idToken = user.authentication.idToken else { return } 
    guard let accessToken = user.authentication.accessToken else { return } 
    let credentials = FIRGoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken) 

    FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in 
     if let error = error { 
     print("Faild to create a firebase user with google account: ", error) 
     return 
     } 

     guard let uid = user?.uid else { return } 
     print("Successfully logged into firebase with google ", uid) 
     // redirect to the next screen after successful login 

    }) 
    } 
} 
+0

因此,如果这应该是我AppDelegate.swift,对于视图控制器我应该只是有'IBAction'调用'GIDSignIn.sharedInstance()签到()'? – MarksCode

+0

@MarksCode更新我的回答,这样你就可以在特定视图控制器 – ronatory

+0

你的救星,由于使用的方法,在按钮的IBAction为! – MarksCode