[swift] ChameleonFramework의 소셜 로그인 기능

ChameleonFramework은 Swift를 위한 UI 강화 도구입니다. 이 프레임워크를 사용하여 소셜 로그인 기능을 구현할 수 있습니다. 이 글에서는 ChameleonFramework를 사용하여 Facebook 및 Google 로그인을 구현하는 방법에 대해 알아보겠습니다.

ChameleonFramework 설치

먼저, ChameleonFramework를 프로젝트에 추가해야 합니다. 이를 위해 CocoaPods를 사용할 수 있습니다. Podfile에 다음과 같은 내용을 추가하고 프로젝트를 업데이트하세요.

pod 'ChameleonFramework/Social'

Facebook 로그인

Facebook 로그인을 구현하기 위해선 Facebook 개발자 계정을 생성하고 앱을 등록해야 합니다. 등록한 후에는 앱 ID를 얻을 수 있습니다. 이제 iOS 프로젝트에 Facebook 로그인 기능을 추가하겠습니다.

  1. AppDelegate.swift 파일을 열고 UIApplicationDelegate를 구현한 후에 다음 코드를 추가합니다:

    import FBSDKLoginKit
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        ...
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
            ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
            return true
        }
    
        ...
    }
    
  2. ViewController.swift 파일을 열고, 다음 코드를 추가하여 Facebook 로그인 버튼을 만듭니다:

    import FBSDKLoginKit
    
    class ViewController: UIViewController {
    
        ...
    
        let loginButton = FBLoginButton()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            loginButton.center = view.center
            view.addSubview(loginButton)
        }
    
        ...
    }
    

    위 코드에서 FBLoginButton은 ChameleonFramework의 FBSDKLoginKit에서 제공되는 클래스입니다.

  3. 마지막으로, Info.plist 파일에 Facebook 로그인을 위한 앱 ID를 추가해야 합니다. 이를 위해 다음 코드를 파일에 추가하세요:

    <key>FacebookAppID</key>
    <string>YOUR_APP_ID</string>
    <key>FacebookDisplayName</key>
    <string>YOUR_APP_NAME</string>
    

    여기서 YOUR_APP_IDYOUR_APP_NAME은 Facebook 개발자 계정에서 얻은 앱 ID와 앱 이름으로 대체해야 합니다.

Google 로그인

Google 로그인을 구현하기 위해서는 Google 개발자 콘솔에서 앱을 등록하고 클라이언트 ID를 발급받아야 합니다. 이제 iOS 프로젝트에 Google 로그인 기능을 추가하도록 하겠습니다.

  1. Xcode에서 프로젝트를 열고, Info.plist 파일을 선택하여 편집합니다.
  2. 다음 코드를 파일에 추가하세요:

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLName</key>
            <string></string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>com.googleusercontent.apps.YOUR_CLIENT_ID</string>
            </array>
        </dict>
    </array>
    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>google</string>
        <string>googlechrome</string>
        <string>ghttps</string>
    </array>
    

    여기서 YOUR_CLIENT_ID는 Google 개발자 콘솔에서 얻은 클라이언트 ID로 대체해야 합니다.

  3. AppDelegate.swift 파일을 열고, UIApplicationDelegate를 구현한 후에 다음 코드를 추가합니다:

    import GoogleSignIn
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        ...
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
            GIDSignIn.sharedInstance().clientID = "YOUR_CLIENT_ID"
            return true
        }
       
        ...
    
        func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
            return GIDSignIn.sharedInstance().handle(url)
        }
    
        ...
    }
    

    이 코드는 앱 시작 시 Google 로그인을 설정하고, URL 핸들링을 위한 메소드를 추가합니다.

  4. ViewController.swift 파일을 열고, 다음 코드를 추가하여 Google 로그인 버튼을 만듭니다:

    import GoogleSignIn
       
    class ViewController: UIViewController {
    
        ...
    
        let signInButton = GIDSignInButton()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            signInButton.center = view.center
            view.addSubview(signInButton)
        }
    
        ...
    }
    

    여기서 GIDSignInButton은 ChameleonFramework의 GoogleSignIn에서 제공되는 클래스입니다.

위와 같이 ChameleonFramework를 사용하여 Facebook 및 Google 로그인 기능을 구현할 수 있습니다. 자세한 내용은 ChameleonFramework 및 Facebook, Google 개발자 문서를 참조하세요.

참고 자료