LoginSignup
8
8

More than 5 years have passed since last update.

【Obj-C】Firebaseのplistを環境ごとに切り替えるコード例

Last updated at Posted at 2016-11-09

FirebaseからダウンロードしたGoogleService-Info.plistを環境ごとに切り替えるコードを紹介します.
Swift版はこちらを参考にしてください.
FirebaseをStaging環境とかDebug環境とかRelease環境で切り替えをする

今回はDebugの場合とReleaseの場合とその他の場合でplistを分岐させます.
Debug → GoogleService-Info-Debug.plist
Release → GoogleService-Info-Release.plist
その他 → GoogleService-Info.plist

コード例

普段,[FIRApp configure]で済ませる箇所にoptionを追加します.

AppDelegate.h
#import <UIKit/UIKit.h>
#import "Firebase.h"  // ←追加部分:Firebaseをインポートする

@interface AppDelegate : UIResponder <UIApplicationDeledate>

@property (strong, nonatomic) UIWindow *window;

@end
AppDelegate.m
#import "AppDelegate.h"

/*
 * 環境ごとに使用するplist名を定義しておく
 * plist名 → firebasePlistName
*/
#ifdef DEBUG
static NSString * const firebasePlistName = @"GoogleService-Info-Debug";
#elif RELEASE
static NSString * const firebasePlistName = @"GoogleService-Info-Release";
#else
static NSString * const firebasePlistName = @"GoogleService-Info";
#endif

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    NSString *filePath =[[NSBundle mainBundle] pathForResource:firebasePlistName ofType:@"plist"];
    FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:filePath];

    [FIRApp configureWithOptions:options];

    return YES;
}




@end

こんな感じで環境ごとにplistを切り替えることができます.
環境別に分析したいという時にお役に立てれば幸いです.

関連記事:Firebase Analyticsを本番環境とテスト環境で使い分ける[GAみたいにできないの?]

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8