ios 실전 앱 만들기 : 어떤 앱이 만들어 질까
logincontroller.swift
// Copyright © 2018년 MacBookPro. All rights reserved.
// t\색상68cd4c
import UIKit
import Firebase
class LoginController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
//사진 선택했는지 체크
var picCheck = false
//피커뷰 데이터
let gender = ["남자","여자"]
var age:[String] = []
var messagesController: MessageController?
//피커뷰 객체
let genderPickerView :UIPickerView = {
let pick = UIPickerView()
pick.tag = 1
return pick
}()
//나이 피커뷰 객체
let agePickerView :UIPickerView = {
let pick = UIPickerView()
pick.tag = 2
return pick
}()
//피커뷰 상속 메서드
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
//피커뷰 row 개수
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var countrows : Int = age.count
if (pickerView == genderPickerView){
countrows = gender.count
}
return countrows
}
//피커뷰 제목
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == genderPickerView {
let titleRow = gender[row]
return titleRow
} else if pickerView == agePickerView {
let titleRow = age[row]
return titleRow
}
return ""
}
//피커뷰를 선택했을 때
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == genderPickerView {
let titleRow = gender[row]
genderSegmentControl.text = titleRow
genderSegmentControl.resignFirstResponder()
} else if pickerView == agePickerView {
let titleRow = age[row]
ageTextField.text = titleRow
ageTextField.resignFirstResponder()
}
}
//컨테이너 뷰
let inputsContainerView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.black
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 5
view.layer.masksToBounds = true
return view
}()
//로그인 버튼
lazy var loginRegisterButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
button.setTitle("가입ㄱㄱ", for: UIControlState())
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitleColor(.black, for: UIControlState())
button.titleLabel?.font = UIFont(name: "SDMiSaeng", size: 30)
button.layer.cornerRadius = 5
button.layer.masksToBounds = true
button.addTarget(self, action: #selector(handleLoginRegister), for: .touchUpInside)
return button
}()
@objc func handleLoginRegister(){
if loginRegisterSegmentedControl.selectedSegmentIndex == 0 {
handleLogin()
}else{
handleRegister()
}
}
//로그인 버튼 액션
func handleLogin(){
guard let email = emailTextField.text, let password = passwordTextField.text else {
print("값이 없거나 잘못된 형식")
return
}
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
if let error = error {
print(error)
return
}
//로그인 할때 네비게이션 바 데이터 변경 함수 호출
self.messagesController?.fetchUserAndSetupNavBarTitle()
//로그인 성공시 로그인창 내려주기
self.dismiss(animated: true, completion: nil)
}
}
//닉네임 텍스트 필드
let nameTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "어렸을때 별명ㅋㅋ"
if let placeholder = tf.placeholder {
tf.attributedPlaceholder = NSAttributedString(string:placeholder,
attributes: [NSAttributedStringKey.foregroundColor: UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)])
}
tf.font = UIFont(name: "SDMiSaeng", size: 25)
tf.textColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
tf.tintColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
tf.translatesAutoresizingMaskIntoConstraints = false
return tf
}()
//닉네임 구분선
let nameSeparatorView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
//이메일 텍스트 필드
let emailTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "비번 찾을때 이메일"
if let placeholder = tf.placeholder {
tf.attributedPlaceholder = NSAttributedString(string:placeholder,
attributes: [NSAttributedStringKey.foregroundColor: UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)])
}
tf.font = UIFont(name: "SDMiSaeng", size: 25)
tf.textColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
tf.tintColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
tf.translatesAutoresizingMaskIntoConstraints = false
return tf
}()
//이메일 구분선
let emailSeparatorView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
//패스워드 텍스트 필드
let passwordTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "비밀번호는 여섯자리 이상"
if let placeholder = tf.placeholder {
tf.attributedPlaceholder = NSAttributedString(string:placeholder,
attributes: [NSAttributedStringKey.foregroundColor: UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)])
}
tf.font = UIFont(name: "SDMiSaeng", size: 25)
tf.textColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
tf.tintColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
tf.translatesAutoresizingMaskIntoConstraints = false
tf.isSecureTextEntry = true
return tf
}()
//패스워드 구분선
let passwordSeparatorView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
//성별
lazy var genderSegmentControl: UITextField = {
let tf = UITextField()
tf.placeholder = "남자 여자?"
if let placeholder = tf.placeholder {
tf.attributedPlaceholder = NSAttributedString(string:placeholder,
attributes: [NSAttributedStringKey.foregroundColor: UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)])
}
tf.font = UIFont(name: "SDMiSaeng", size: 25)
tf.textColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
tf.tintColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
tf.translatesAutoresizingMaskIntoConstraints = false
return tf
}()
//성별 구분선
let genderSeperatorView:UIView = {
let view = UIView()
view.backgroundColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
//나이
lazy var ageTextField:UITextField = {
let tf = UITextField()
tf.placeholder = "나이"
if let placeholder = tf.placeholder {
tf.attributedPlaceholder = NSAttributedString(string:placeholder,
attributes: [NSAttributedStringKey.foregroundColor: UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)])
}
tf.font = UIFont(name: "SDMiSaeng", size: 25)
tf.textColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
tf.tintColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
tf.translatesAutoresizingMaskIntoConstraints = false
return tf
}()
//프로필 이미지
lazy var profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "main2")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
//클릭시 이벤트 등록
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView)))
imageView.isUserInteractionEnabled = true
return imageView
}()
lazy var loginRegisterSegmentedControl:UISegmentedControl = {
let sc = UISegmentedControl(items: ["로그인","가입ㄱㄱ"])
sc.translatesAutoresizingMaskIntoConstraints = false
let font = UIFont(name: "SDMiSaeng", size: 20)
sc.setTitleTextAttributes([NSAttributedStringKey.font: font as Any],
for: .normal)
sc.tintColor = UIColor(red:0.41, green:0.80, blue:0.30, alpha:1.0)
sc.selectedSegmentIndex = 1
sc.addTarget(self, action: #selector(handleLoginRegisterChange), for: .valueChanged)
return sc
}()
//로그인 등록 세그먼트
@objc func handleLoginRegisterChange(){
let title = loginRegisterSegmentedControl.titleForSegment(at: loginRegisterSegmentedControl.selectedSegmentIndex)
loginRegisterButton.setTitle(title, for: UIControlState())
let containerHeight = loginRegisterSegmentedControl.selectedSegmentIndex == 0 ? 100 : 200
inputsContainerViewHeightAnchor?.constant = CGFloat(containerHeight)
//이름 필드 높이
nameTextFieldHeightAnchor?.isActive = false
nameTextFieldHeightAnchor = loginRegisterSegmentedControl.selectedSegmentIndex == 0 ? nameTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 0) : nameTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/5)
nameTextFieldHeightAnchor?.isActive = true
//이메일 필드 높이
emailTextFieldHeightAnchor?.isActive = false
emailTextFieldHeightAnchor = loginRegisterSegmentedControl.selectedSegmentIndex == 0 ? emailTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/2) : emailTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/5)
emailTextFieldHeightAnchor?.isActive = true
//비밀번호 필드 높이
passwordTextFieldHeightAnchor?.isActive = false
passwordTextFieldHeightAnchor = loginRegisterSegmentedControl.selectedSegmentIndex == 0 ? passwordTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/2) : passwordTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/5)
passwordTextFieldHeightAnchor?.isActive = true
//성별 높이
genderSegmentControlHeightAnchor?.isActive = false
genderSegmentControlHeightAnchor = loginRegisterSegmentedControl.selectedSegmentIndex == 0 ? genderSegmentControl.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 0) : genderSegmentControl.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/5)
genderSegmentControlHeightAnchor?.isActive = true
//나이 높이
ageTextFieldHeightAnchor?.isActive = false
ageTextFieldHeightAnchor = loginRegisterSegmentedControl.selectedSegmentIndex == 0 ? ageTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 0) : ageTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/5)
ageTextFieldHeightAnchor?.isActive = true
}
//진입점
override func viewDidLoad() {
super.viewDidLoad()
//나이 피커뷰에 데이터 넣어주기
for i in 8...99{
self.age.append("\(i)")
}
//바깥탭 -> 키보드 숨기기
self.hideKeyboard()
//피커뷰 델리게이트
genderPickerView.delegate = self
genderPickerView.dataSource = self
agePickerView.delegate = self
agePickerView.dataSource = self
view.backgroundColor = .black
view.addSubview(inputsContainerView)
view.addSubview(loginRegisterButton)
view.addSubview(profileImageView)
view.addSubview(loginRegisterSegmentedControl)
//입력 객체
setupInputsContainerView()
//로그인버튼 객체
setupLoginRegisterButton()
//이미지뷰 객체
setupProfileImageView()
//로그인 세그먼트 컨트롤
setupLoginRegisterSegmentedControl()
}
//로그인등록 버튼 세그먼트 제약조건
func setupLoginRegisterSegmentedControl(){
loginRegisterSegmentedControl.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
loginRegisterSegmentedControl.bottomAnchor.constraint(equalTo: inputsContainerView.topAnchor, constant: -15).isActive = true
loginRegisterSegmentedControl.heightAnchor.constraint(equalToConstant: 30).isActive = true
loginRegisterSegmentedControl.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
}
//높이 제약조건
var inputsContainerViewHeightAnchor: NSLayoutConstraint?
var nameTextFieldHeightAnchor: NSLayoutConstraint?
var emailTextFieldHeightAnchor: NSLayoutConstraint?
var passwordTextFieldHeightAnchor: NSLayoutConstraint?
var genderSegmentControlHeightAnchor: NSLayoutConstraint?
var ageTextFieldHeightAnchor: NSLayoutConstraint?
//컨테이너 뷰 제약조건 설정
func setupInputsContainerView(){
//컨테이너 뷰 제약조건
inputsContainerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
inputsContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
inputsContainerView.widthAnchor.constraint(equalTo:view.widthAnchor,constant:-24).isActive = true
inputsContainerViewHeightAnchor = inputsContainerView.heightAnchor.constraint(equalToConstant: 200)
inputsContainerViewHeightAnchor?.isActive = true
//컨테이너 뷰 안에 뷰객체 넣기
inputsContainerView.addSubview(nameTextField)
inputsContainerView.addSubview(nameSeparatorView)
inputsContainerView.addSubview(emailTextField)
inputsContainerView.addSubview(emailSeparatorView)
inputsContainerView.addSubview(passwordTextField)
inputsContainerView.addSubview(passwordSeparatorView)
inputsContainerView.addSubview(genderSegmentControl)
genderSegmentControl.inputView = genderPickerView
inputsContainerView.addSubview(genderSeperatorView)
inputsContainerView.addSubview(ageTextField)
ageTextField.inputView = agePickerView
//뷰객체 제약조건 설정
//이름
nameTextField.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor, constant: 12).isActive = true
nameTextField.topAnchor.constraint(equalTo: inputsContainerView.topAnchor, constant: 0).isActive = true
nameTextField.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor, constant: 0).isActive = true
nameTextFieldHeightAnchor = nameTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/5)
nameTextFieldHeightAnchor?.isActive = true
//이름 구분선
nameSeparatorView.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor).isActive = true
nameSeparatorView.topAnchor.constraint(equalTo: nameTextField.bottomAnchor).isActive = true
nameSeparatorView.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
nameSeparatorView.heightAnchor.constraint(equalToConstant: 1).isActive = true
//이메일
emailTextField.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor, constant: 12).isActive = true
emailTextField.topAnchor.constraint(equalTo: nameTextField.bottomAnchor, constant: 0).isActive = true
emailTextField.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor, constant: 0).isActive = true
emailTextFieldHeightAnchor = emailTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/5)
emailTextFieldHeightAnchor?.isActive = true
//이메일 구분선
emailSeparatorView.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor).isActive = true
emailSeparatorView.topAnchor.constraint(equalTo: emailTextField.bottomAnchor).isActive = true
emailSeparatorView.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
emailSeparatorView.heightAnchor.constraint(equalToConstant: 1).isActive = true
//비밀번호
passwordTextField.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor, constant: 12).isActive = true
passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: 0).isActive = true
passwordTextField.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor, constant: 0).isActive = true
passwordTextFieldHeightAnchor = passwordTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/5)
passwordTextFieldHeightAnchor?.isActive = true
//비밀번호 구분선
passwordSeparatorView.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor).isActive = true
passwordSeparatorView.topAnchor.constraint(equalTo: passwordTextField.bottomAnchor).isActive = true
passwordSeparatorView.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
passwordSeparatorView.heightAnchor.constraint(equalToConstant: 1).isActive = true
//성별
genderSegmentControl.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor, constant: 12).isActive = true
genderSegmentControl.topAnchor.constraint(equalTo: passwordTextField.bottomAnchor, constant: 0).isActive = true
genderSegmentControl.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor, constant: 0).isActive = true
genderSegmentControlHeightAnchor = genderSegmentControl.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/5)
genderSegmentControlHeightAnchor?.isActive = true
//성별 구분선
genderSeperatorView.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor).isActive = true
genderSeperatorView.topAnchor.constraint(equalTo: genderSegmentControl.bottomAnchor).isActive = true
genderSeperatorView.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
genderSeperatorView.heightAnchor.constraint(equalToConstant: 1).isActive = true
//나이
ageTextField.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor, constant: 12).isActive = true
ageTextField.topAnchor.constraint(equalTo: genderSegmentControl.bottomAnchor, constant: 0).isActive = true
ageTextField.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor, constant: 0).isActive = true
ageTextFieldHeightAnchor = ageTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/5)
ageTextFieldHeightAnchor?.isActive = true
}
//로그인버튼 제약 조건 설정
func setupLoginRegisterButton(){
loginRegisterButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
loginRegisterButton.topAnchor.constraint(equalTo: inputsContainerView.bottomAnchor, constant: 12).isActive = true
loginRegisterButton.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
loginRegisterButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
}
//프로필 이미지 제약 조건 설정
func setupProfileImageView(){
profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
profileImageView.bottomAnchor.constraint(equalTo: loginRegisterSegmentedControl.topAnchor, constant: -75).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 60).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 60).isActive = true
}
}
extension UIColor {
convenience init(r: CGFloat, g: CGFloat, b: CGFloat) {
self.init(red: r/255, green: g/255, blue: b/255, alpha: 1)
}
}
//바탕화면 텝 했을 때 키보드 숨기기
extension UIViewController
{
func hideKeyboard()
{
let tap: UITapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard()
{
view.endEditing(true)
}
}
LoginController+handlers.swift
// Copyright © 2018년 MacBookPro. All rights reserved.
//
import UIKit
import Firebase
extension LoginController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//등록 버튼 액션
func handleRegister(){
if(picCheck){
//유효성 검사
guard let email = emailTextField.text, let password = passwordTextField.text, let name = nameTextField.text,let gender = genderSegmentControl.text, let age = ageTextField.text else {
//유효성 검사 해주기
print("값이 없거나 잘못된 형식")
return
}
//파이어베이스 가입
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
//에러 발생
if let error = error{
if let errCode = AuthErrorCode(rawValue: error._code) {
switch errCode {
case .invalidEmail:
let alert = UIAlertController(title: "경고 ", message:"이메일 형식이 아닌것 같음..", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "ㅇㅇ", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
case .wrongPassword :
let alert = UIAlertController(title: "경고 ", message:"비밀번호 확인 ㄱㄱ", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "ㅇㅇ", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
case .accountExistsWithDifferentCredential :
let alert = UIAlertController(title: "경고 ", message:"이메일을 확인좀..", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "ㅇㅇ", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
case .userNotFound :
let alert = UIAlertController(title: "경고 ", message:"존재하지 않는 이메일이여", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "ㅇㅇ", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
default:
let alert = UIAlertController(title: "경고 ", message:"빈칸없는지 다시바바ㅠㅠ", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "ㅇ", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
return
}
//가입후 uid 넘겨준다. uid 가 nil이면 return
guard let uid = user?.uid else {
return
}
//유저 가입 성공후
//유일한 스트링 값
let imageName = NSUUID().uuidString
//firebase 저장소 위치 가져오기
let storageRef = Storage.storage().reference().child("profile_images").child("\(imageName).jpg")
//image 파일 변환
//더 안전하게
if let profileImage = self.profileImageView.image, let uploadData = UIImageJPEGRepresentation(profileImage, 0.1){
//업로드
storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
if let error = error {
print(error)
return
}
//이미지가 저장된 url 가져온 후 users db에 삽입
if let profileImageUrl = metadata?.downloadURL()?.absoluteString {
let values = ["name": name, "email": email, "profileImageUrl": profileImageUrl,"gender":gender,"age":age]
//최종 저장 함수 호출(데이터 넘겨줌)
self.registerUserIntoDatabaseWithUID(uid, values: values as [String : AnyObject])
}
})
}
}
}else{
let alert = UIAlertController(title: "경고 ", message:"맨위에 바바 사진넣어야됨...", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "ㅇㅇ", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
//최종적으로 유저 저장
fileprivate func registerUserIntoDatabaseWithUID(_ uid: String, values: [String: AnyObject]) {
let ref = Database.database().reference()
let usersReference = ref.child("users").child(uid)
//최종 저장
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
if let err = err {
print(err)
return
}
let user = User(dic:values)
//등록 할때 메시지 컨트롤러에 있는 setupnavbar 함수 호출
self.messagesController?.setupNavBarWithUser(user: user)
self.dismiss(animated: true, completion: nil)
})
picCheck = false
}
//프로필 이미지 클릭했을 때 실행되는 이벤트 함수
@objc func handleSelectProfileImageView(){
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
present(picker, animated: true, completion: nil)
}
//이미지 피커 선택 완료
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
selectedImageFromPicker = editedImage
} else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker {
profileImageView.image = selectedImage
picCheck = true
}
dismiss(animated: true, completion: nil)
}
//이미지 피커 취소
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
mainviewcontroller.swift
//
// MainViewController.swift
// DatingApp
//
// Created by MacBookPro on 2018. 5. 15..
// Copyright © 2018년 MacBookPro. All rights reserved.
//
import UIKit
import Firebase
class MainViewController: UIViewController {
var ref: DatabaseReference!
var mainViewController: MainViewController?
//진입점
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "로그아웃", style: .plain, target:self , action: #selector(handleLogout))
//로그인or로그아웃 체크
checkIfUserIsLoggedIn()
}
//로그인or로그아웃 체크 함수
func checkIfUserIsLoggedIn(){
//로그아웃 되었을 때 실행
if Auth.auth().currentUser?.uid == nil{
perform(#selector(handleLogout), with: nil, afterDelay: 0)
}else{
//로그인 되었으면 네비게이션 타이틀의 제목을 유저 이름으로 지정해준다.
fetchUserAndSetupNavBarTitle()
}
}
func fetchUserAndSetupNavBarTitle(){
//로그인 되었으면 네비게이션 타이틀의 제목을 유저 이름으로 지정해준다.
guard let uid = Auth.auth().currentUser?.uid else{
//uid가
return
}
ref = Database.database().reference()
ref.child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
print("타이틀 바 표시 함수 호출 성공-")
print(snapshot)
if let dictionary = snapshot.value as? [String: AnyObject]{
let user = User(dic: dictionary)
self.setupNavBarWithUser(user: user)
}
}, withCancel: nil)
}
//네비게이션 타이틀 바 변경해주기
func setupNavBarWithUser(user: User){
print("사진있는 타이틀! 호출")
let titleView = MyUIView()
titleView.frame = CGRect(x:0, y:0, width: 100, height: 50)
//이미지와 라벨을 담을 뷰 객체
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
titleView.addSubview(containerView)
//이미지 객체
let profileImageView = UIImageView()
profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.contentMode = .scaleAspectFill
profileImageView.layer.cornerRadius = 20
profileImageView.clipsToBounds = true
if let profileImageUrl = user.profileImageUrl{
profileImageView.loadImageUsingCacheWithUrlString(profileImageUrl)
}
containerView.addSubview(profileImageView)
profileImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
profileImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 35).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 35).isActive = true
let nameLable = UILabel()
containerView.addSubview(nameLable)
nameLable.text = user.name
nameLable.translatesAutoresizingMaskIntoConstraints = false
nameLable.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
nameLable.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true
nameLable.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
nameLable.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = true
//nameLable.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showChatController)))
containerView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true
self.navigationItem.titleView = titleView
}
//로그아웃 액션
@objc func handleLogout(){
do{
try Auth.auth().signOut()
} catch let logError{
print(logError)
}
mainViewController?.fetchUserAndSetupNavBarTitle()
let loginController = LoginController()
present(loginController, animated: true, completion: nil)
}
}
myuiview.swift
import UIKit
class MyUIView: UIView {
override var intrinsicContentSize: CGSize {
return UILayoutFittingExpandedSize
}
}
Extensions.swift
//
// Extensions.swift
// DatingApp
//
//
// Copyright © 2018년 MacBookPro. All rights reserved.
//
import UIKit
let imageCache = NSCache<NSString, AnyObject>()
extension UIImageView{
//새 메시지 보내기 테이블 뷰에서 호출하는 이미지 캐쉬 함수
func loadImageUsingCacheWithUrlString(_ urlString: String) {
self.image = nil
//이미지 캐쉬가 있는지 확인 후 있으면 return
if let cachedImage = imageCache.object(forKey: urlString as NSString) as? UIImage {
self.image = cachedImage
return
}
//이미지 캐치가 없으면 다운로드
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
//다운로드 중 에러발생, 종료
if let error = error {
print(error)
return
}
DispatchQueue.main.async(execute: {
//이미지 캐쉬에 넣기
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: urlString as NSString)
self.image = downloadedImage
}
})
}).resume()
}
}
//
// User.swift
// DatingApp
//
// Copyright © 2018년 MacBookPro. All rights reserved.
//
import UIKit
class User: NSObject {
var name : String?
var email : String?
var profileImageUrl: String?
var id : String?
init(dic:[String:Any]){
self.name = dic["name"] as? String ?? ""
self.email = dic["email"] as? String ?? ""
self.profileImageUrl = dic["profileImageUrl"] as? String
self.id = dic["id"] as? String
}
}
'ios 뽀개기 > 실전' 카테고리의 다른 글
instagram like (0) | 2018.03.06 |
---|---|
alert 커스텀 슬라이더 (0) | 2018.01.03 |
alert 커스텀 - 이미지 (0) | 2018.01.03 |
alert 커스텀 - 지도 2 (0) | 2018.01.03 |
alert 커스텀 - 지도1 (0) | 2018.01.03 |
댓글