FirebaseUI Authを使って最低限のログイン機能を実装します。
今回はGoogleログインとAppleログインの2つを実装します。
Firebaseのプロジェクトは作成できている前提です。
1. XcodeにFirebase Authenticationを追加する
今回はcocoapodsでFirebaseを導入します。
podfileに以下を追加し、
pod 'Firebase/Analytics'
pod 'FirebaseUI'
pod install
します。
2. Firebaseのプロジェクト上でAuthenationを有効にする
左側のタブからAuthenation
を探し、始める
を押します
このような画面になるはずです
今回はGoogleとAppleの認証を行うので、
2-1. Googleを有効にする
Google
を押し、
有効にする
のチェックをオンにします
公開名
とサポートメール
を入力し、保存
を押します
次に、GoogleSercvice-Info.plist
を開き、REVERSED_CLIENT_ID
の内容をコピーし、
Info
内のURL Types
内のURL Schemes
にペーストします
2-2. Appleを有効にする
新しいプロバイダを追加
を押し、
Apple
を選択します
有効にする
のチェックをオンにし、保存
を押します
Signing & Capabilities
で+ Capability
を選択し、
Sign in with Apple
をドラッグ&ドロップして追加します
(リリースするときに必要)
Appleの開発者サイトのCertificates, Identifiers & Profilesページを開き、アプリのSign In with Appleを有効にします。
3. Xcodeで設定する
import UIKit
+ import FirebaseCore
+ import FirebaseAuthUI
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
+ FirebaseApp.configure()
return true
}
import UIKit
+ import FirebaseAuthUI
+ import FirebaseOAuthUI
+ import FirebaseGoogleAuthUI
+ class ViewController: UIViewController, FUIAuthDelegate {
+ let authUI = FUIAuth.defaultAuthUI()!
override func viewDidLoad() {
super.viewDidLoad()
+ let providers: [FUIAuthProvider] = [
+ FUIGoogleAuth(authUI: authUI),
+ FUIOAuth.appleAuthProvider()
+ ]
+ self.authUI.delegate = self
+ self.authUI.providers = providers
}
+ @IBAction func LoginButtonTapped(){
+ let authViewController = self.authUI.authViewController()
+ self.present(authViewController, animated: true, completion: nil)
+ }
+ public func authUI(_ authUI: FUIAuth, didSignInWith user: User?, error: Error?){
+ if error == nil {
+ print("成功")
+ } else {
+ print(error!)
+ }
+ }
}
これでできます!お疲れ!
参考