0
ユーザーのGoogleプロフィール画像を取得してFirebaseデータベースに保存できません。空の画像をFirebaseデータベースに保存するだけです。私のコードは次のようになります。Googleプロフィール画像を取得して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)
}
}