dev/ios

[iOS] Firebase 파이어베이스 연결(RealtimeDatabase)

neander 2023. 10. 19. 17:09
728x90
반응형

파이어베이스 연결 목적은 버전 비교를 하기 위함

 

 

1. GoogleService-Info.plist 추가

: 본인의 어떤 앱인지 식별하여서 파이어베이스 서버가 알기 위함

: 프로젝트 설정 / SDK안내

 

 

2. Firebase SDK추가

: Firebase에서 제공하는 기능들을 사용하기 위함

: (Xcode) File / Add Package Dependencies...

 

 

다음 문장 검색 후 Add Package클릭

https://github.com/firebase/firebase-ios-sdk

 

 

Add Package

 

 

3. RealtimeDatabase 설정

: AppDelegate에 FirebaseAPI를 사용하겠다 선언

import FirebaseCore

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        // Firebase library to configure APIs
        FirebaseApp.configure()
        
        return true
    }
}

 

 

파이어베이스 데이터베이스의 값을 가져오기 위한 객체 생성

import FirebaseDatabase

class MainViewController: UIViewController {

    // Firebase RealtimeDatabase
    private var ref: DatabaseReference!
    
    
    ...
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        // Firebase Init
        ref = Database.database().reference()
    }
}

 

 

4. Database읽기

링크 : https://firebase.google.com/docs/database/ios/read-and-write?hl=ko&authuser=0

Firebase RealtimeDatabase

    private func firebaseRealtimeDatabaseInit() {
        ref = Database.database().reference()
        ref.observe(DataEventType.value, with: { snapshot in
            print("ref observe... snapshot:\(snapshot)")
        })
        
        ref.child("version-ios").getData(completion: { error, snapshot in
            guard error == nil else {
                // fail
                print("version-ios error:\(error!.localizedDescription)")
                return
            }
            
            // success
            let versionServer = snapshot?.value as? String ?? "Unknown"
            print("version-ios:\(versionServer)")
        })
    }

 

[log]

ref observe... snapshot:Snap ((null)) {
    test = "1.0.2";
    version = "1.0.6";
    "version-aos" = "1.0.6";
    "version-ios" = "1.0.6";
}
version-ios:1.0.6

 

 

+ 5. 자신의 앱 버전 불러오기

guard let info = Bundle.main.infoDictionary,
      let versionLocal = info["CFBundleShortVersionString"] as? String else {
	  	  return
      }
          
print("info:\(info)")
print("versionLocal:\(versionLocal)")

[log]

info:["CFBundleShortVersionString": 1.0.3, "CFBundleVersion": 3, ...
versionLocal:1.0.3
728x90
반응형