[iOS]Firebase Remote Config

chaaansooo·2022년 5월 14일
0

iOS

목록 보기
5/8
post-thumbnail

Firebase Remote Config

Firebase Remote Config의 필요성

앱을 출시하고, 긴급하게 수정해야할 사항이 생길 때 앱 수정을 하고 심사를 받아서 배포를 하기 위해서는 너무 오랜 시간이 걸린다.
이때 우리는 Firebase의 Romote Config를 사용해서 앱을 수정할 수 있다.

Firebase Remote Config이란?

  • 배포, 업데이트 없이 앱을 변경할 수 있도록 해준다
  • 기본값 설정 후 값을 재정의할 수 있다.
  • 클라우드 기반 key-value 저장소이다.
  • 앱 버전, 언어 등으로 분류된 사용자에게 세그먼트별 환경을 제공한다

사용해보기

사전 준비

  1. Firebase의 프로젝트에서 "참여"에 있는 Remote config에 들어간다.
  2. Remote Config 설정을 하고 시작을 해준다.
  3. 프로젝트에 plist를 추가하여 우리가 필요한 key-value를 설정해주고, firebase 내에도 동일하게 key를 만들어주고 value를 설정해준다.

코드

    override func viewDidLoad() {
        super.viewDidLoad()
        
        remoteConfig = RemoteConfig.remoteConfig()
        
        let setting = RemoteConfigSettings()
        setting.minimumFetchInterval = 0 //test를 위해서 패치되는 인터벌 얼만큼?
        
        remoteConfig?.configSettings = setting //setting값 전달
        remoteConfig?.setDefaults(fromPlist: "RemoteConfigDefaultsPropertyList")
    }
  • 뷰컨의 viewDidLoad에 remoteconfig 인스턴스를 만들어주고, 우리가 원하는 setting값을 설정해준다.
    func getNotice(){
        guard let remoteConfig = remoteConfig else {return}
        remoteConfig.fetch{ [weak self] status, _ in
            if status == .success{
                remoteConfig.activate()
            } else{
                print("ERROR: Config not fetched")
            }
            
            guard let self = self else { return }
            if !self.isNoticeHeidden(remoteConfig){
                let noticeVC = NoticeViewController(nibName: "NoticeViewController", bundle: nil)
                
                noticeVC.modalPresentationStyle = .custom
                noticeVC.modalTransitionStyle = .crossDissolve
                
                let title = (remoteConfig["title"].stringValue ?? "").replacingOccurrences(of: "\\n", with: "\n")
                let detail = (remoteConfig["detail"].stringValue ?? "").replacingOccurrences(of: "\\n", with: "\n")
                let date = (remoteConfig["date"].stringValue ?? "").replacingOccurrences(of: "\\n", with: "\n")
                
                noticeVC.noticeContents = (title: title, detail: detail, date: date)
                self.present(noticeVC, animated: true, completion: nil)
            } else{
                self.showEventAlert()
            }
        }
    }
  • viewWillAppear()에 들어갈 함수를 만들어준다.
  • fetch 메소드를 사용하여 Remote Config에서 설정한 값들을 불러온다
  • Remote Config에서 받을 값들로 우리가 하고자하는 것을 진행한다.

Firebase A/B Testing의 필요성

규모가 작은 앱을 운영을 할 때는 데이터를 기반으로 사용자의 니즈를 파악하기가 힘들다.
이때 A/B Testing은 비교군의 데이터와 실험군의 데이터를 다르게 하여 사용자에게 보여주고, 사용자의 행동을 파악하여 쉽게 사용자의 니즈를 파악할 수 있도록 해준다.

사용해보기

사용자에게 두가지 alert를 보여주고, 어떤 alert창이 더 확인을 많이 누르는지를 볼 수 있는 코드를 작성해볼 것이다.

//AB test
extension ViewController{
    func showEventAlert(){
        guard let remoteConfig = remoteConfig else {return}
        remoteConfig.fetch{ [weak self] status, _ in
            if status == .success{
                remoteConfig.activate()
            } else {
                print("Config not fetched")
            }
            let message = remoteConfig["message"].stringValue ?? ""
            let confirmAction = UIAlertAction(title: "확인하기", style: .default){ _ in
                Analytics.logEvent("promotion_alert", parameters: nil)
            }
            
            let cancelAction = UIAlertAction(title: "취소", style: .cancel)
            
            let alertController = UIAlertController(title: "깜짝이벤트", message: message, preferredStyle: .alert)
            
            alertController.addAction(confirmAction)
            alertController.addAction(cancelAction)
            
            self?.present(alertController, animated: true)
        }
    }
}
  • Remote Config 내의 A/B Testing에 추가한 message라는 key 값을 불러오고, message 값을 담아서 alert 창을 띄우는 함수이다.
  • 여기서 Firebase Remote Config는 우리가 설정한대로 사용자들에게 다른 value 값을 준다.
  • confirmAction 프로퍼티의 클로저에 Analytics.logEvent를 통해 confirmAction이 일어날 경우 우리는 firebase에서 사용자의 행동을 파악할 수 있다.
profile
악으로 깡으로 버티기

0개의 댓글