포스트

Final (3)

Social Login

1. Kakao Login 구현

CleanShot 2024-06-02 at 16 04 37@2x

우선 Kakao Developer에 가서 로그인을 활성화로 설정.

AppDelegate 설정 -> 초기화를 해준다.

1
2
3
4
5
6
7
8
import KakaoSDKCommon
...

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

  KakaoSDK.initSDK(appKey: "${NATIVE_APP_KEY}")

}

SceneDelegate 설정.

1
2
3
4
5
6
7
 func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        if let url = URLContexts.first?.url {
            if (AuthApi.isKakaoTalkLoginUrl(url)) {
                _ = AuthController.handleOpenUrl(url: url)
            }
        }
    }

VM 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func kakaoLoginDidTapped() {
        if (UserApi.isKakaoTalkLoginAvailable()) {
            UserApi.shared.loginWithKakaoAccount {[weak self] (oauthToken, error) in
                if let error = error {
                    self?.loginPublisher.send(completion: .failure(error))
                }
                else {
                    print("loginWithKakaoTalk() success.")
                    self?.loginPublisher.send()
                    //do something
                    _ = oauthToken
                }
            }
        }
    }

우선은 기본적으로 제시되어있는거로만 해둔 상태.

그리고 publisher를통해 전달만 하게 해두었다.

그리고 VC에서 기존에 하던대로 연결을 해두었지만 실행이 되지는 않는 상태이다.

아무래도

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func kakaoLoginDidTapped() {
        print("tapped")
        if (UserApi.isKakaoTalkLoginAvailable()) {
            UserApi.shared.loginWithKakaoAccount {[weak self] (oauthToken, error) in
                if let error = error {
                    self?.loginPublisher.send(completion: .failure(error))
                }
                else {
                    self?.loginPublisher.send()
                    print("loginWithKakaoTalk() success.")
                    //do something
                    _ = oauthToken
                }
            }
        }
    }

여기에서 실행이 안되는 것같다.

혹시나해서 tapped를 프린트 해보았는데 출력은 되는 상태.

찾아보니 카카오톡이 설치되어야만 isKakaoTalkLoginAvailable이게 호출이 되는듯하다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
func kakaoLoginDidTapped() {
        print("tapped")
        if (UserApi.isKakaoTalkLoginAvailable()) {
            UserApi.shared.loginWithKakaoTalk {[weak self] (oauthToken, error) in
                if let error = error {
                    self?.loginPublisher.send(completion: .failure(error))
                    print(error)
                }
                else {
                    self?.loginPublisher.send()
                    print("loginWithKakaoTalk() success.")
                    //do something
                    _ = oauthToken
                }
            }
        } else {
            UserApi.shared.loginWithKakaoAccount {[weak self] (oauthToken, error) in
                if let error = error {
                    self?.loginPublisher.send(completion: .failure(error))
                    print(error)
                }
                else {
                    self?.loginPublisher.send()
                    print("loginWithKakaoTalk() success.")
                    //do something
                    _ = oauthToken
                }
            }
        }
    }

카카오톡이 설치되어있을 경우 아닐경우로 나누었다.

Jun-02-2024 18-36-23

완료.

db 이전은 네이버, 구글 완료이후에 하는걸로.

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.