Skip to content

Commit

Permalink
#46 애플 로그인 연동 1
Browse files Browse the repository at this point in the history
  • Loading branch information
doyeonk429 committed Aug 17, 2024
1 parent 2af3b8b commit 080191a
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 51 deletions.
13 changes: 4 additions & 9 deletions Drink-EG/Drink-EG.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
16B97D132C4C0F4100D8B453 /* ClassVideoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassVideoViewController.swift; sourceTree = "<group>"; };
16B97D152C4C0F6400D8B453 /* SavingVideoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SavingVideoViewController.swift; sourceTree = "<group>"; };
16B97D172C4C0F6E00D8B453 /* SearchVideoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchVideoViewController.swift; sourceTree = "<group>"; };
16C19AA12C70723200E93FD0 /* Drink-EG.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "Drink-EG.entitlements"; sourceTree = "<group>"; };
1F205A302C68BB5200E80659 /* CartListCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CartListCollectionViewCell.swift; sourceTree = "<group>"; };
1F205A322C69FE9800E80659 /* JoinNLoginRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JoinNLoginRequest.swift; sourceTree = "<group>"; };
1F205A362C6A71BC00E80659 /* APIResponseLoginResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIResponseLoginResponse.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -252,7 +253,6 @@
files = (
5E2219712C6B5B5B005849FB /* Cosmos in Frameworks */,
5E22195E2C69DDB5005849FB /* SnapKit in Frameworks */,
165401162C6E3858008A9DBF /* CardSlider in Frameworks */,
5E2219672C69DE78005849FB /* KakaoSDK in Frameworks */,
5E22197B2C6BB56F005849FB /* SDWebImage in Frameworks */,
5E2219612C69DDBF005849FB /* Alamofire in Frameworks */,
Expand Down Expand Up @@ -363,6 +363,7 @@
16B97CD22C4BEB0900D8B453 /* Drink-EG */ = {
isa = PBXGroup;
children = (
16C19AA12C70723200E93FD0 /* Drink-EG.entitlements */,
5E22196B2C6B2416005849FB /* Info.plist */,
16B97CEA2C4BF36300D8B453 /* Resources */,
16B97CF02C4BF3F100D8B453 /* Sources */,
Expand Down Expand Up @@ -634,7 +635,6 @@
5E2219662C69DE78005849FB /* KakaoSDK */,
5E2219692C69DEEA005849FB /* Toast */,
5E2219702C6B5B5B005849FB /* Cosmos */,
165401152C6E3858008A9DBF /* CardSlider */,
5E22197A2C6BB56F005849FB /* SDWebImage */,
5E22197C2C6BB56F005849FB /* SDWebImageMapKit */,
);
Expand Down Expand Up @@ -673,7 +673,6 @@
5E2219652C69DE78005849FB /* XCRemoteSwiftPackageReference "kakao-ios-sdk" */,
5E2219682C69DEEA005849FB /* XCRemoteSwiftPackageReference "Toast-Swift" */,
5E22196F2C6B5B5B005849FB /* XCRemoteSwiftPackageReference "Cosmos" */,
165401142C6E3858008A9DBF /* XCRemoteSwiftPackageReference "cardslider" */,
5E2219792C6BB56F005849FB /* XCRemoteSwiftPackageReference "SDWebImage" */,
);
productRefGroup = 16B97CD12C4BEB0900D8B453 /* Products */;
Expand Down Expand Up @@ -721,9 +720,7 @@
5E2219552C69D5E7005849FB /* CustomSlider.swift in Sources */,
1654010B2C6BACDC008A9DBF /* SelectionManager.swift in Sources */,
16752F9D2C5A7FB90001907D /* HexCode.swift in Sources */,
165401192C6E555A008A9DBF /* CardSliderViewController.swift in Sources */,
1F598F0B2C5CBD9E000CE79F /* EnterTasteTestViewController.swift in Sources */,
165401212C6E56BD008A9DBF /* RatingView.swift in Sources */,
1F8F99FA2C6F538300EAEF6C /* WineInfoResponse.swift in Sources */,
5E22196E2C6B2EA0005849FB /* RatingViewController.swift in Sources */,
1F205A432C6B66EB00E80659 /* TasteTestThirdCollectionViewCell.swift in Sources */,
Expand All @@ -739,15 +736,11 @@
1F598EFD2C58150E000CE79F /* JoinViewController.swift in Sources */,
165400E32C69D6B9008A9DBF /* CallMoyaExample.swift in Sources */,
165400E72C69DDBE008A9DBF /* TastingNoteAPI.swift in Sources */,
1654011F2C6E56A9008A9DBF /* UIViewExtension.swift in Sources */,
1654010F2C6E2B4B008A9DBF /* MyCollectionViewController.swift in Sources */,
1654011D2C6E5681008A9DBF /* CardsLayout.swift in Sources */,
16B97CEC2C4BF37E00D8B453 /* Constant.swift in Sources */,
16B97D182C4C0F6E00D8B453 /* SearchVideoViewController.swift in Sources */,
1F598EEC2C524D2E000CE79F /* RecomCollectionViewCell.swift in Sources */,
165401232C6E57BC008A9DBF /* CardSliderCell.swift in Sources */,
1654010D2C6C7FBB008A9DBF /* APIResponseMemberResponse.swift in Sources */,
1654011B2C6E5661008A9DBF /* CardTitleview.swift in Sources */,
1F8F99F52C6F40F400EAEF6C /* APIResponseWineSearchResponse.swift in Sources */,
165401072C6BA5D8008A9DBF /* MemberInfoRequest.swift in Sources */,
1F205A372C6A71BC00E80659 /* APIResponseLoginResponse.swift in Sources */,
Expand Down Expand Up @@ -945,6 +938,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = "Drink-EG/Drink-EG.entitlements";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 83K577A944;
Expand Down Expand Up @@ -972,6 +966,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = "Drink-EG/Drink-EG.entitlements";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 83K577A944;
Expand Down
10 changes: 10 additions & 0 deletions Drink-EG/Drink-EG/Drink-EG.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.applesignin</key>
<array>
<string>Default</string>
</array>
</dict>
</plist>
111 changes: 82 additions & 29 deletions Drink-EG/Drink-EG/Sources/VCs/Login/LoginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
import UIKit
import SnapKit
import Moya
import AuthenticationServices


class LoginViewController: UIViewController, UITextFieldDelegate {

class LoginViewController: UIViewController, UITextFieldDelegate, ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {
public static var isFirstLogin : Bool = true

let provider = MoyaProvider<LoginAPI>()
public var userID : String?
public var userPW : String?
var loginDTO : JoinNLoginRequest?

let loginButton = UIButton(type: .system)
let joinButton = UIButton(type: .system)

let kakaoButton = UIButton(type: .system)
let appleButton = UIButton(type: .system)
let appleButton = ASAuthorizationAppleIDButton(type: .signIn, style: .whiteOutline)

let idStoreButton = UIButton(type: .custom)
private let checkImage = UIImage(named: "icon_check_fill")
Expand Down Expand Up @@ -72,7 +72,7 @@ class LoginViewController: UIViewController, UITextFieldDelegate {

override func viewDidLoad() {
super.viewDidLoad()

self.navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named:"icon_back")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named:"icon_back")
Expand All @@ -84,7 +84,7 @@ class LoginViewController: UIViewController, UITextFieldDelegate {
titleLabel.font = UIFont.boldSystemFont(ofSize: 20)
titleLabel.textColor = .white
titleLabel.textAlignment = .center

let titleView = UIView()
titleView.addSubview(titleLabel)
titleLabel.snp.makeConstraints { make in
Expand All @@ -93,7 +93,7 @@ class LoginViewController: UIViewController, UITextFieldDelegate {
}

self.navigationItem.titleView = titleView

view.backgroundColor = .black

idTextField.delegate = self
Expand Down Expand Up @@ -126,24 +126,24 @@ class LoginViewController: UIViewController, UITextFieldDelegate {
}

let buttonStackView = UIStackView(arrangedSubviews: [kakaoButton, appleButton])
buttonStackView.axis = .horizontal
buttonStackView.axis = .vertical
buttonStackView.distribution = .fillEqually
buttonStackView.spacing = 13

view.addSubview(buttonStackView)
buttonStackView.snp.makeConstraints { make in
make.top.equalTo(view).offset(571)
make.leading.trailing.equalToSuperview().inset(110)
make.top.equalTo(joinStackView.snp.bottom).offset(50)
make.leading.trailing.equalToSuperview().inset(100)
make.centerX.equalToSuperview()
make.width.equalTo(173)
make.height.equalTo(60)
make.width.greaterThanOrEqualTo(173)
make.height.greaterThanOrEqualTo(60)
}

let idStoreStackView = UIStackView(arrangedSubviews: [idStoreButton, idStoreLabel])
idStoreStackView.axis = .horizontal
idStoreStackView.distribution = .fillProportionally
idStoreStackView.spacing = 5

view.addSubview(idStoreStackView)
idStoreStackView.snp.makeConstraints { make in
make.top.equalTo(view).offset(368)
Expand Down Expand Up @@ -261,7 +261,7 @@ class LoginViewController: UIViewController, UITextFieldDelegate {
private func configureKakaoButton() {
//카카오 이미지를 아이콘 포멧 이미지에 맞게 바꿈
kakaoButton.setImage(UIImage(named: "kakao")?.withRenderingMode(.alwaysOriginal), for: .normal)

kakaoButton.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0.26, alpha: 0.5)
kakaoButton.layer.cornerRadius = 16
kakaoButton.layer.borderWidth = 2
Expand All @@ -270,15 +270,68 @@ class LoginViewController: UIViewController, UITextFieldDelegate {
}

private func configureAppleButton() {
appleButton.setImage(UIImage(named: "apple")?.withRenderingMode(.alwaysOriginal), for: .normal)
appleButton.tintColor = .white
appleButton.addTarget(self, action: #selector(handleAuthorizationAppleIDButtonPress), for: .touchUpInside)
}

//MARK: - Apple Login delegate
@objc
func handleAuthorizationAppleIDButtonPress() {
let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]

appleButton.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0.26, alpha: 0.5)
appleButton.layer.cornerRadius = 16
appleButton.layer.borderWidth = 2
appleButton.layer.borderColor = UIColor.white.cgColor.copy(alpha: 0.1)
let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
}

func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return self.view.window!
}

func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
switch authorization.credential {
case let appleIDCredential as ASAuthorizationAppleIDCredential:
let userIdentifier = appleIDCredential.user
let fullName = appleIDCredential.fullName
let email = appleIDCredential.email

if let authorizationCode = appleIDCredential.authorizationCode,
let identityToken = appleIDCredential.identityToken,
let authCodeString = String(data: authorizationCode, encoding: .utf8),
let identityTokenString = String(data: identityToken, encoding: .utf8) {
print("authorizationCode: \(authorizationCode)\n")
print("identityToken: \(identityToken)\n")
print("authCodeString: \(authCodeString)\n")
print("identityTokenString: \(identityTokenString)\n")
}

print("useridentifier: \(userIdentifier)")
print("fullName: \(fullName)")
print("email: \(email)")

// move to MainPage
goToNextView()

case let passwordCredential as ASPasswordCredential:
// Sign in using an existing iCloud Keychain credential.
let username = passwordCredential.user
let password = passwordCredential.password

print("username: \(username)")
print("password: \(password)")
default:
break
}
}

func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
// 로그인 실패(유저의 취소도 포함)
print("login failed - \(error.localizedDescription)")
}


private func configureIdStoreButton() {
idStoreButton.setImage(ncheckImage?.withRenderingMode(.alwaysOriginal), for: .normal)
idStoreButton.backgroundColor = .clear
Expand All @@ -288,7 +341,7 @@ class LoginViewController: UIViewController, UITextFieldDelegate {
@objc private func idStoreButtonTapped(_ sender: UIButton) {
// Bool 값 toggle
sender.isSelected.toggle()

// 버튼이 클릭될 때마다, 버튼 이미지를 변환
if sender.isSelected {
sender.setImage(checkImage?.withRenderingMode(.alwaysOriginal), for: .selected)
Expand Down Expand Up @@ -349,7 +402,7 @@ class LoginViewController: UIViewController, UITextFieldDelegate {
textField.setPwIcon(UIImage(named: "icon_lock_fill")!)
}
}

func textFieldDidEndEditing(_ textField: UITextField) {
// 텍스트 필드가 선택 해제되었을 때 배경색 원래대로
textField.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0.26, alpha: 0.5)
Expand Down Expand Up @@ -397,11 +450,11 @@ class LoginViewController: UIViewController, UITextFieldDelegate {
switch result {
case .success(let response):
if let httpResponse = response.response,
let setCookie = httpResponse.allHeaderFields["Set-Cookie"] as? String {
let setCookie = httpResponse.allHeaderFields["Set-Cookie"] as? String {
let cookies = HTTPCookie.cookies(withResponseHeaderFields: ["Set-Cookie": setCookie], for: httpResponse.url!)

for cookie in cookies {
// print("Cookie Name: \(cookie.name), Value: \(cookie.value)")
// print("Cookie Name: \(cookie.name), Value: \(cookie.value)")
HTTPCookieStorage.shared.setCookie(cookie)
}
}
Expand All @@ -422,7 +475,7 @@ extension UITextField : UITextFieldDelegate {

func setIdIcon(_ image: UIImage) {
if let iconContainerView = leftView,
let iconView = iconContainerView.viewWithTag(UITextField.idIconTag) as? UIImageView {
let iconView = iconContainerView.viewWithTag(UITextField.idIconTag) as? UIImageView {
iconView.image = image
} else {
let iconView = UIImageView(frame: CGRect(x: 26, y: -9, width: 16, height: 16))
Expand All @@ -434,10 +487,10 @@ extension UITextField : UITextFieldDelegate {
leftViewMode = .always
}
}

func setPwIcon(_ image: UIImage) {
if let iconContainerView = leftView,
let iconView = iconContainerView.viewWithTag(UITextField.pwIconTag) as? UIImageView {
let iconView = iconContainerView.viewWithTag(UITextField.pwIconTag) as? UIImageView {
iconView.image = image
} else {
let iconView = UIImageView(frame: CGRect(x: 28, y: -10, width: 13.5, height: 18))
Expand Down
Loading

0 comments on commit 080191a

Please sign in to comment.