获取Google个人资料图片并将其保存到Firebase中

时间:2017-04-03 20:55:56

标签: ios firebase firebase-authentication google-signin firebase-storage

我无法获取用户的Google个人资料图片并将其保存到Firebase数据库。它只是将空白图片保存到Firebase数据库。我的代码如下所示:

Appdelegate.swift

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
    if let err = error {
        print ("Failed to log in with Google: ", err)
        return
    }

    print ("Successfully logged in with 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 err = error {
            print("Failed to create a Firebase User with Google: ", err)
            return
        } else {


        guard let uid = user?.uid else { return }
        print("Sucessfully logged in to Firebase with Google", uid)
            //sends user to homepage VC after a successful login
            let myStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let homePage = myStoryboard.instantiateViewController(withIdentifier: "homePageVC") as! homePageViewController


            let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.window?.rootViewController = homePage

        }

    })

homepageViewController.swift

@IBOutlet weak var profilePic: UIImageView!

func firebaseStorage() {

    let user = FIRAuth.auth()?.currentUser
    if let user = user {

        let name = user.displayName

        self.profileName.text = name

    }
    // Get a reference to the storage service using the default Firebase App
    let storage = FIRStorage.storage()

    // Create a storage reference
    let storageRef = storage.reference()

    //points to the child directory where the profile picture will be saved on firebase
    let profilePicRef = storageRef.child("/User Profile Pictures/"+(user?.uid)!+"/profile_pic.jpg")

 if (GIDSignIn.sharedInstance().currentUser != nil) {


            let data = Data()

            //upload image to storage
            let uploadTask = profilePicRef.put(data, metadata: nil) { (metadata, error) in
                guard let metadata = metadata else {
                    // Uh-oh, an error occurred!
                    return
                }
                // Metadata contains file metadata such as size, content-type, and download URL.
                let downloadURL = metadata.downloadURL
            }

            self.profilePic.image = UIImage(data: Data)

        }

   }

1 个答案:

答案 0 :(得分:1)

别介意!我设法通过为感兴趣的人修改视图控制器代码来实现它:

if (GIDSignIn.sharedInstance().currentUser != nil) {

            let imageUrl = GIDSignIn.sharedInstance().currentUser.profile.imageURL(withDimension: 400).absoluteString
            let url  = NSURL(string: imageUrl) as! URL
            let data = NSData(contentsOf: url)

            //upload image to storage
            let uploadTask = profilePicRef.put(data as! Data, metadata: nil) { (metadata, error) in
                guard let metadata = metadata else {
                    // Uh-oh, an error occurred!
                    return
                }
                // Metadata contains file metadata such as size, content-type, and download URL.
                let downloadURL = metadata.downloadURL
            }

            self.profilePic.image = UIImage(data: data as! Data)

        }


    }