Skip to content

Instantly share code, notes, and snippets.

@yotubarail
Last active February 10, 2020 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yotubarail/5f81cf29ad2a05df3f600bf18038d755 to your computer and use it in GitHub Desktop.
Save yotubarail/5f81cf29ad2a05df3f600bf18038d755 to your computer and use it in GitHub Desktop.
FIrebaseのdisplayNameとpictureURLの取得・更新
import UIKit
import Firebase
import SDWebImage
class accountViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {
let firebaseAuth = Auth.auth()
let imagePick = UIImagePickerController()
@IBOutlet weak var profImage: UIImageView!
@IBOutlet weak var profName: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
imagePick.delegate = self
// プロフィール画像読み込み
SDWebImageManager.shared.loadImage(with: firebaseAuth.currentUser!.photoURL, options: .highPriority, progress: nil) { (image, data, error, cacheType, isFinished, imageUrl) in
self.profImage.image = image
}
// プロフィール名前読み込み
profName.setTitle(firebaseAuth.currentUser!.displayName, for: .normal)
}
@IBAction func changeProfImage(_ sender: Any) {
imagePick.sourceType = .photoLibrary
imagePick.modalPresentationStyle = .fullScreen
present(imagePick, animated: true, completion: nil)
}
// カメラロールから写真を選ぶ
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
dismiss(animated: true, completion: nil)
//保存のアラートを出す
let alert: UIAlertController = UIAlertController(title: "保存しますか?", message: "Want to save?", preferredStyle: UIAlertController.Style.alert)
// OKの場合
let defaultAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:{
(action: UIAlertAction!) -> Void in
print("OK")
if let pickedImage = info[.originalImage] as? UIImage {
self.profImage.contentMode = .scaleAspectFit
self.profImage.image = pickedImage
}
self.upload()
})
//キャンセルの場合
let cancelAction: UIAlertAction = UIAlertAction(title: "キャンセル", style: UIAlertAction.Style.cancel, handler:{
(action: UIAlertAction!) -> Void in
print("選択をキャンセルしました")
})
alert.addAction(defaultAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
// Firebaseにアップロード
fileprivate func upload() {
let storageRef = Storage.storage().reference(forURL: "gs://XXX.com").child("users").child("profile").child("profImage.jpg")
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
if let uploadData = self.profImage.image?.jpegData(compressionQuality: 0.3) {
storageRef.putData(uploadData, metadata: metaData) { (metadata , error) in
if error != nil {
print("error: \(error!.localizedDescription)")
return
}
storageRef.downloadURL(completion: { (url, error) in
if error != nil {
print("error: \(error!.localizedDescription)")
}
print("url: \(url!.absoluteString)")
let changeRequest = self.firebaseAuth.currentUser?.createProfileChangeRequest()
if let photoURL = URL(string: url!.absoluteString){
changeRequest?.photoURL = photoURL
}
changeRequest?.commitChanges { (error) in
// ...
}
})
}
}
}
// 写真を選ぶのをキャンセル
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
print("キャンセルされました")
}
@IBAction func changeProfName(_ sender: Any) {
let alert: UIAlertController = UIAlertController(title: "名前を変更します", message: "", preferredStyle: UIAlertController.Style.alert)
// OKの場合
let defaultAction: UIAlertAction = UIAlertAction(title: "変更", style: UIAlertAction.Style.default, handler:{
(action: UIAlertAction!) -> Void in
self.profName.setTitle(alert.textFields?.first?.text, for: .normal)
let changeRequest = self.firebaseAuth.currentUser?.createProfileChangeRequest()
changeRequest?.displayName = alert.textFields?.first?.text
changeRequest?.commitChanges { (error) in
// ...
}
})
//キャンセルの場合
let cancelAction: UIAlertAction = UIAlertAction(title: "キャンセル", style: UIAlertAction.Style.cancel, handler:{
(action: UIAlertAction!) -> Void in
print("変更をキャンセルしました")
})
alert.addAction(defaultAction)
alert.addAction(cancelAction)
//textfiledの追加
alert.addTextField(configurationHandler: {(text:UITextField!) -> Void in
text.placeholder = "新しい名前"
})
self.present(alert, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment