-
Notifications
You must be signed in to change notification settings - Fork 0
[week18] OAuth
Hemg edited this page Sep 23, 2023
·
1 revision
- 로그인
@IBAction func loginAction(_ sender: UIButton) {
// 카카오 설치 여부 확인하는 메서드
if (UserApi.isKakaoTalkLoginAvailable()) {
UserApi.shared.loginWithKakaoTalk { (oauthToken, error) in
if let error = error {
print(error)
} else {
print("loginWithKakaoTalk() success.")
_ = oauthToken
// 위에 만든 메서드인데 로그인을 통해 사용자 토근을 발급하고 API호출
self.setUserInfo()
}
}
}
}
- 시뮬레이터가 없을때 사파리로 카카오 접속하기 메서드
@IBAction func loginAction(_ sender: UIButton) {
UserApi.shared.loginWithKakaoAccount {(oauthToken, error) in
if let error = error {
print(error)
}
else {
print("loginWithKakaoAccount() success.")
_ = oauthToken
self.setUserInfo()
}
}
}
private func setUserInfo() {
UserApi.shared.me { (user, error) in
if let error = error {
print(error)
} else {
print("nickname: \(user?.kakaoAccount?.profile?.nickname ?? "no nickname")")
print("email: \(user?.kakaoAccount?.email ?? "no email")")
guard let userId = user?.id else {return}
print("닉네임 : \(user?.kakaoAccount?.profile?.nickname ?? "no nickname").....이메일 : \(user?.kakaoAccount?.email ?? "no nickname"). . . . .유저 ID : \(userId)")
self.nickNamaLabel.text = "Nickname : \(user?.kakaoAccount?.profile?.nickname ?? "no nickname")"
self.emailLabel.text = "Email : \(user?.kakaoAccount?.email ?? "no nickname")"
}
}
}
🔗 참고 링크