Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用谷歌Firebase身份验证注册时向Firestore添加用户数据使用Swift登录_Ios_Swift_Firebase_Google Cloud Firestore_Firebase Authentication - Fatal编程技术网

Ios 使用谷歌Firebase身份验证注册时向Firestore添加用户数据使用Swift登录

Ios 使用谷歌Firebase身份验证注册时向Firestore添加用户数据使用Swift登录,ios,swift,firebase,google-cloud-firestore,firebase-authentication,Ios,Swift,Firebase,Google Cloud Firestore,Firebase Authentication,当用户在我的iOS应用程序上首次登录谷歌时,我需要在Firestore的“用户”集合中存储一些数据。我正在使用Firebase身份验证。要存储的数据是: id:用户的UID 显示名称:用户的全名 photoURL:用户的google帐户的url 点数:当用户首次登录时,该值将为0 knownLanguageCodes:当用户首次登录时,这将是一个空数组 目前,没有任何东西保存到Firestore。 这是“我的应用程序代理”中处理登录的部分 class AppDelegate: UIRespo

当用户在我的iOS应用程序上首次登录谷歌时,我需要在Firestore的“用户”集合中存储一些数据。我正在使用Firebase身份验证。要存储的数据是:

  • id:用户的UID
  • 显示名称:用户的全名
  • photoURL:用户的google帐户的url
  • 点数:当用户首次登录时,该值将为0
  • knownLanguageCodes:当用户首次登录时,这将是一个空数组
目前,没有任何东西保存到Firestore。 这是“我的应用程序代理”中处理登录的部分

class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {

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

        FirebaseApp.configure()
        GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
        GIDSignIn.sharedInstance().delegate = self

        return true
    }

    //Only available on iOS 9 and later
    @available(iOS 9.0, *)
    func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url)
    }

    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) { 
        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)

        Auth.auth().signIn(with: credential) { (res, err) in

            if let err = err {
                print(err.localizedDescription)
                return
            }

            //I'm not sure if this block of code is in the right place
            let db = Firestore.firestore()
            db.collection("user").document(String((res?.user.uid)!)).setData([
                "id" : String((res?.user.uid)!),
                "displayName" : (res?.user.displayName)!,
                "photoURL" : (res?.user.photoURL)!
                "points" : 0
                "knownLanguageCodes" : []
            ], merge: true)
        } 
    }

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

    }
    //...

我正在使用SwiftUI作为UI。

是的,一切都是正确的,但您没有调用此函数
GIDSignIn.sharedInstance().signIn()

是,一切正常,但您缺少调用此函数
GIDSignIn.sharedInstance().signIn()

AppDelegate
相比,最好在
UIViewController
中对
UIButton
的操作执行此过程。我认为您缺少
GIDSignIn.sharedInstance()?.signIn()
,因此您可以为
ui按钮
操作尝试以下代码

//rest of your code

if GIDSignIn.sharedInstance()?.hasPreviousSignIn() == true {

   //perform Your action if a user already sign in

} else {

  GIDSignIn.sharedInstance()?.signIn()

} 

//rest of your code

与其使用
AppDelegate
,不如在
UIViewController
中的
UIButton
操作中执行此过程。我认为您缺少
GIDSignIn.sharedInstance()?.signIn()
,因此您可以为
ui按钮
操作尝试以下代码

//rest of your code

if GIDSignIn.sharedInstance()?.hasPreviousSignIn() == true {

   //perform Your action if a user already sign in

} else {

  GIDSignIn.sharedInstance()?.signIn()

} 

//rest of your code