быстро, как получить информацию о пользователе из входа в Google? - PullRequest
0 голосов
/ 29 мая 2020

Я могу выполнить вход в делегате приложения и даже распечатать данные, но после этого я не могу получить информацию от пользователя где-либо еще. Я пытался получить доступ к GIDGoogleUser, но, похоже, не так, имя всегда равно нулю. Я выделил свои сбои в ViewController

делегат моего приложения:

import UIKit
import GoogleSignIn

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {



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

        // Initialize sign-in
        GIDSignIn.sharedInstance().clientID = "myId"
        GIDSignIn.sharedInstance().delegate = self

        return true
    }

    @available(iOS 9.0, *)
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
      return GIDSignIn.sharedInstance().handle(url)
    }
    //for iOS 8
    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
        print("TEST full name:", fullName ?? "N/A")
      // ...

    }

    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!,
              withError error: Error!) {
      // Perform any operations when the user disconnects from app here.
      // ...
    }

мой контроллер

import UIKit
import GoogleSignIn

class ViewController: UIViewController {

    @IBOutlet weak var signIgn: GIDSignInButton!

    override func viewDidLoad() {
        super.viewDidLoad()

//        let name = GIDGoogleUser().profile.name
//        debugPrint("TEST in viewDidLoad:", name) //CRASHES HERE
        GIDSignIn.sharedInstance()?.presentingViewController = self

        //CRASHES HERE
        let name = GIDSignIn.sharedInstance().currentUser.profile
        debugPrint("TEST in viewDidLoad name is", name )
    }
    @IBAction func loginTapped(_ sender: Any) {
        GIDSignIn.sharedInstance()?.signIn()
    }

    @IBAction func logoutTapped(_ sender: Any) {
        GIDSignIn.sharedInstance().signOut()
    }

}
...