Skip to content

iosアプリとfirebaseの連携です。iosのアプリからfirebase上のデータの追加・更新…ができます。

License

Notifications You must be signed in to change notification settings

heroccccc/FirebaseiOS

Repository files navigation

FirebaseiOS

iosアプリとfirebaseの連携です。iosのアプリからfirebase上のデータの追加・更新…ができます。


Firebaseとは…

googleが提供しているMBaas(Mobile Backend as a Service)

オンラインのデータベースにデータを保存・取得などができる

みなさんわかりやすくまとめてます

今回はFirebaseをiOSで使用してみました


準備

1. firebaseのプロジェクトの作成

こちらからFirebaseのデータベースを登録していきます.

新しいプロジェクトを作成します.作成すると次に、プロジェクト名等を入力してください.

2. firebaseのプロジェクトの設定

下の図のように順をおって入力していってください.

iosハンドルIDはfirebaseを使用したいアプリのIDを使用します.下の図の青枠の部分を使用してください.

3. iOSアプリにfirebaseSDKの追加

2の途中でCocoaPodsでinstallする流れとなっていますが、少々使用方法によってinstallが異なるため注意してください.今回は以下の2つをinstallすることにしました.いくつかあります

target 'FirebaseiOS' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  pod 'Firebase/Core'
  pod 'Firebase/Database'

  # Pods for FirebaseiOS

end

4.Databaseのルール設定

firebaseのプロジェクトのデータベースの、ルールより以下の通りに変更し、公開ボタンをおす.

5.iOSアプリの初期設定(AppDelegate.swiftの設定)

AppDelegate.swiftとはこちらより

2つ追加する

import UIKit

//追加
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        //追加
        FirebaseApp.configure()
        return true
    }
    //以下省略…

準備完了!!!

1度ビルドしてみてください.


使用

1.ViewController.swiftの編集

databaseの更新を行っています.

import UIKit

//追加
import Firebase

class ViewController: UIViewController {

    //firebaseに追加するtext入力
    @IBOutlet weak var inputText: UITextField!

    // インスタンス変数
    var DBRef:DatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //インスタンスを作成
        DBRef = Database.database().reference()

    }

    //sendボタンが押されたら
    @IBAction func sendFirebase(_ sender: Any) {

        //databaseにあげるデータ
        let data = ["name": inputText.text!]

        //databaseにあげるデータ更新
        DBRef.child("/").updateChildValues(data)


    }
    //以下省略…

2.ビルドして確認


アプリからdatabaseの変更・更新などをリアルタイムにできる!!!

About

iosアプリとfirebaseの連携です。iosのアプリからfirebase上のデータの追加・更新…ができます。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published