[iOS/Swift] 카카오톡 로그인 AccessToken 자동 갱신

프로그래밍/swift2020. 4. 7. 17:07

제가 Swift에서.. 카카오 토큰의 자동갱신에 대해 잘못알고 있었습니다.

 

토큰이 만료되면 자동으로 갱신이 되는 줄 알았는데 그게 아니네요 ㅜㅜ

그냥 갱신되는 게 아니라 보통 API call을 하면 SDK내부적으로 자동 갱신이 된다고 합니다.

(https://devtalk.kakao.com/t/handledidbecomeactive/17663/4)

 

그런데 여기서 말하는 API call는.. 카카오 SDK의 카카오 함수들인데

사실 이 함수들은.. 로그인할 때 말고는 잘 사용되지 않습니다.

 

그래서 자동으로 갱신이 되는 방법이 필요했습니다.

다행히.. 이 방법이 있습니다. 찾아보니 2015년에 공개된 Kakao SDK부터 포함된 기능이네요;;

 

automaticPeriodicRefresh를 사용하면 됩니다.

 

AppDelegate.swift

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        KOSession.shared()?.isAutomaticPeriodicRefresh = true
        
        return true
    }
    
     func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        
        KOSession.handleDidEnterBackground()
    }

이를 위해선 AppDeletega.swift 파일에 코드 2줄만 추가하면 됩니다.

위 코드를 참고해주세요.

 

실제로 자동갱신이 잘 되는지는.. 좀 지켜봐야겠습니다.

작성자

Posted by 드리머즈

관련 글

댓글 영역