Google SignIn集成问题

时间:2018-04-24 06:50:10

标签: ios swift integration google-signin

我试图在我的代码中实现。当用户完成登录过程时,我将如何转到另一个控制器。但我没有在谷歌上获得任何完美的解决方案。我能为这类问题做些什么。如果您对我有任何建议,请帮助。

这是ViewController.swift代码我逐步放入所有代码。

import UIKit
import GoogleSignIn

class ViewController: UIViewController, GIDSignInUIDelegate, GIDSignInDelegate {

    @IBOutlet weak var btnOutLet: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        GIDSignIn.sharedInstance().clientID = "KEY"

        GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().delegate = self 
    }

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

        if error != nil
        {
            print(error)
            return
        }
        else
        {
            print(user.profile.email)
            //self.performSegue(withIdentifier: "", sender: self)
        } 
    }

    @IBAction func btnAction(_ sender: Any) { 
        GIDSignIn.sharedInstance().signIn()  
    }

}

这是Appdelegate代码

import UIKit
import GoogleSignIn

@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

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

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

}

2 个答案:

答案 0 :(得分:0)

用户登录后,您的应用会获得所需的信息,您可以使用:

func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {
    self.viewController!.dismiss(animated: true, completion: nil)
}

其中一个GIDSignInUIDelegate,在用户签名后,它将起作用,因为您可以使用它导航到任何视图控制器。

答案 1 :(得分:0)

试用此代码

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

    if error == nil
    {

        print(user.profile.email)

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.35) {
            self.performSegue(withIdentifier: "youridentifier", sender: self)
        }


    }
    else
    {
        print(error)
    }
}

func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!) {
    self.present(viewController, animated: true, completion: nil)
}

func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {
    viewController.dismiss(animated: true, completion: nil)
}