Gid didSignInFor实际在哪里登录用户?

时间:2019-04-18 15:44:33

标签: ios swift google-signin

我正在尝试将用户图片保存到Firebase存储中,将其恢复为downloadUrl并将其全部保存到CoreData。我调用了上传功能,并在完成时调用了CoreData保存功能。除了一点点细节,我都能正常工作:如果我从GID didSignInFor调用它,则用户还没有写入storage的权限,因为尚未登录,因此失败。我尝试在执行segue的地方调用这些函数,以为登录过程应该已经完成​​,并且我有权写入Storage,但看不到user。因此,我尝试在最后调用它,但得到的结果是尚未获得许可。您能找出我在didSignInFor做错的地方吗?您可以通过注释掉的代码看到我的尝试。一如既往的感谢。 方法如下:

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
        // ...
        print("User successfully signed in with Google",user)
        guard let idToken = user.authentication.idToken else {return}
        guard let accessToken = user.authentication.accessToken else {return}
        let credentials = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)
        Auth.auth().signInAndRetrieveData(with: credentials) { (user, error) in
            if let error = error {
                print("Failed to create user with Google account", error)
                return
            } else {

                // here user in not accessible yet

                print("Succesfully created new user in Firebase with Google account")
                self.performSegue(withIdentifier: "continueSegue", sender: self)
            }
        }
        // user details
        UserDetails.shopId = user.userID
        UserDetails.fullName = user.profile.name
        UserDetails.givenName = user.profile.givenName
        UserDetails.familyName = user.profile.familyName
        UserDetails.email = user.profile.email

        if user.profile.hasImage {
//            UserDetails.logoUrl = String(describing: user.profile.imageURL(withDimension: 100)) // String // wrong.. use the downloadUrl from Firabase Storage
            let data = try? Data(contentsOf: user.profile.imageURL(withDimension: 100))

            if let imageData = data {
                UserDetails.logo = UIImage(data: imageData)


//                // Post image to Firebase Storage here it fails at first sign in because of storage rules
//                Firebase.uploadImage(completed: { (true) in
//                    // Save user to CoreData
//                    User.saveUserDetails(shopId: UserDetails.shopId!, name: UserDetails.fullName!, address: UserDetails.address ?? "", phoneNumber: UserDetails.phoneNumber ?? "", latitude: UserDetails.latitude ?? "", longitute: UserDetails.longitude ?? "", logo: UserDetails.logo!, logoUrl: UserDetails.logoUrl!)
//                }, UserDetails.logo!)


            }

1 个答案:

答案 0 :(得分:0)

我决定在以下vc的viewWillAppear中进行上传。现在可以根据需要运行了。