점진적 과부하 개발 블로그

Swift Remote Config/ 팝업 제어 본문

Swift

Swift Remote Config/ 팝업 제어

지용빡 2022. 3. 4. 23:51
반응형
Remote Config로 팝업 제어하기
Appdelegate.swift
  • Firebase 연결
import UIKit
import Firebase

@main
class AppDelegate: UIResponder, UIApplicationDelegate {



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

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}
plist 생성

Remote Config
  • Firebase 콘솔로 가서 Remote Config 클릭

  • 구성 만들기

  • plist와 똑같이 매개변수 만들기 

  • Xcode로 돌아가 콘솔에 설정할 값들을 패칭을 한다.
ViewController.swift
//RemoteConfig
extension ViewController {
    func getNotice() {
        guard let remoteConfig = remoteConfig else { return }
        
        remoteConfig.fetch {[weak self] status, _ in
            if status == .success {
                remoteConfig.activate(completion: nil)
            } else {
                print("Config not fetched")
            }
            
            guard let self = self else { return }
            if !self.isNoticeHidden(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)
            }
        }
    }
    
    func isNoticeHidden(_ remoteConfig: RemoteConfig) -> Bool {
        return remoteConfig["isHidden"].boolValue
    }
}
실행 화면

조건부 값 넣기
  • 매개변수 수정 -> 새로 추가

  • 조건부 값 -> 새 조건 만들기

  • 새 조건 정의

  • 다음과 같이 조건을 설정해준다면 사용자의 기기의 언어가 영어일 경우 아래와 같이 조건이 발동되어 Value 값이 출력이 된다.

반응형

'Swift' 카테고리의 다른 글

Swift Local Notification  (0) 2022.03.07
Firebase A/B Test로 팝업 제어  (0) 2022.03.06
Firebase Realtime Database  (0) 2022.03.03
Swift Alamofire  (0) 2022.02.23
OpenWeather API 사용하는 법  (0) 2022.02.17