はじめに
こんばんは。
風呂入って、子供寝かして、ゆっくりしてたら日をまたいでしまいました。。。
とりあえずペナルティ分は書けたからよかったけど、週4は無理でした。
そして今気づいたのですが、はてなの暦モードでみると、日曜が週頭になっちゃうんですね。
なので日曜にブログ書くと次週になっちゃうので、ちょっと嫌だなぁと思っています。
今週から、日〜土の週として考えて、週1ブログを続けてみようと思います。
で、今回は前回の記事の続きで、firebaseのAuthenticationを試してみようと思います。
- 作者: 小笠原みつき,西村公宏,柳佳音,志甫侑紀,池田友洋,木村涼平,?橋優介,大塚雅和,飯塚直,吉川竜太,末永恭正,久保田祐史,浜田真成,穴井宏幸,大島一将,桑原仁雄,牧大輔,池田拓司,はまちや2,竹原,WEB+DB PRESS編集部
- 出版社/メーカー: 技術評論社
- 発売日: 2018/06/23
- メディア: 単行本
- この商品を含むブログを見る
やってみた
はじめのはじめ
公式のドキュメント通りにやってみます。
まずは、プロジェクトフォルダにPodfileを作ります。
pod 'Firebase/Auth'
作成が終わったら、pod installを実行します。
ただ、podコマンドが入ってなかったので、まずcocoapodsをインストールするところからでした。
$ cd test01/ $ gem install cocoapods ... ... ... Installing ri documentation for cocoapods-1.5.3 Done installing documentation for concurrent-ruby, i18n, thread_safe, tzinfo, activesupport, nap, fuzzy_match, cocoapods-core, claide, cocoapods-deintegrate, cocoapods-downloader, cocoapods-plugins, cocoapods-search, cocoapods-stats, netrc, cocoapods-trunk, cocoapods-try, molinillo, atomos, CFPropertyList, colored2, nanaimo, xcodeproj, escape, fourflusher, gh_inspector, ruby-macho, cocoapods after 17 seconds 28 gems installed $ pod setup Setting up CocoaPods master repo $ /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress Cloning into 'master'... remote: Counting objects: 2255401, done. remote: Compressing objects: 100% (125/125), done. remote: Total 2255401 (delta 61), reused 35 (delta 35), pack-reused 2255240 Receiving objects: 100% (2255401/2255401), 550.08 MiB | 2.19 MiB/s, done. Resolving deltas: 100% (1293496/1293496), done. Checking out files: 100% (248932/248932), done. Setup completed $ pod init $ cat Podfile # Uncomment the next line to define a global platform for your project # platform :ios, '9.0' target 'test01' do # Comment the next line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! # Pods for test01 end
インストールできました。
作成したPodfileの Pods for test01
の下に pod 'Firebase/Auth'
を記載して、 pod installを実行します。
$ pod install $ pod install Analyzing dependencies Downloading dependencies Installing Firebase (5.4.0) Installing FirebaseAuth (5.0.2) Installing FirebaseCore (5.0.5) Installing GTMSessionFetcher (1.1.15) Installing GoogleToolboxForMac (2.1.4) Generating Pods project Integrating client project [!] Please close any current Xcode sessions and use `test01.xcworkspace` for this project from now on. Sending stats Pod installation complete! There is 1 dependency from the Podfile and 5 total pods installed. [!] Automatically assigning platform `ios` with version `11.4` on target `test01` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
なにやら警告が出てるっぽいです。
iosのバージョンの指定が無いから、11.4にセットしときましたよ?って感じだと思います。
このままでもよさそうです。
早速作っていきます。
ちなみに起動するのは、プロジェクト名.xcodeproj
ではなく、 プロジェクト名. xcworkspace
を起動します。
そして、AppDelegate.swiftに以下を追加します。
コレで準備万端!
新規登録
さっそく新規登録ボタンに連動するコードを書いていきます!
↓のボタンが押下されたら、FirebaseにAPIを飛ばして、新規ユーザーを追加するような機能を実装します。
RegisterViewController.swiftに以下のIBActionを追加します。
import Firebase @IBAction func registerButton(_ sender: Any) { if (registerMailAddressText.text?.isEmpty == true || registerPasswordText.text?.isEmpty == true) { registerErrorText.text = "メールアドレス or パスワード\nが空文字です。確認してください。" return } registerErrorText.text = "" Auth.auth().createUser(withEmail: registerMailAddressText.text!, password: registerPasswordText.text!) { (authResult, error) in guard let _ = authResult?.user.email, error == nil else { self.registerErrorText.text = error!.localizedDescription return } let storyboard: UIStoryboard = self.storyboard! let mainPage = storyboard.instantiateViewController(withIdentifier: "MainPage") self.present(mainPage, animated: true, completion: nil) } }
おそらくFirebase側で空文字登録は弾いてくれると思いますが、とりあえずやってみました。
実際やってみます。
お、なんかいったっぽい!!!
コンソールも確認します。
きてるー!!!
ログイン
この調子で、ログインも作ってみます。
LoginViewController.swiftに似たようなコードを作ります。
import Firebase @IBAction func registerButton(_ sender: Any) { if (loginMailAddressText.text?.isEmpty == true || loginPasswordText.text?.isEmpty == true) { loginErrorText.text = "メールアドレス or パスワード\nが空文字です。確認してください。" return } loginErrorText.text = "" Auth.auth().signIn(withEmail: loginMailAddressText.text!, password: loginPasswordText.text!) { (user, error) in if let error = error { self.loginErrorText.text = error.localizedDescription return } let storyboard: UIStoryboard = self.storyboard! let mainPage = storyboard.instantiateViewController(withIdentifier: "MainPage") self.present(mainPage, animated: true, completion: nil) } }
ログインボタンに作成したIBActionを連携させて、早速試してみます!
できた!!?
終わりに
エラー制御とか、非アクティブ時の制御など、まだまだ考えないといけないことがありますが、とりあえずAuthenticationはOKということで、終わります!
次は、とった写真をCloud Storageにあげるところやってみます。
おやすみなさいー