LoginSignup
26
25

More than 5 years have passed since last update.

iOS アプリのステータスバーに現在のブランチ名を表示する

Last updated at Posted at 2014-02-07

=============================================

あれこれブランチを切り替えながらひたすらアプリのバグの再現テストをしていると、いまどのブランチのコードを実行しているんだっけ? と分からなくなりわりと焦ります。そこでステータスバーにブランチ名を常時表示させることにして、脳みその負荷をちょこっと下げてみます。

status bar ss

Xcode にスクリプトを追加する

メニューから Editor -> Add Build Phase -> Ad Run Script Build Phase を選び、アプリのターゲットに新しい Run Script を追加します。

xcode ss

次のようなコードを書いて Branch.h を生成するようにします。場所はお好みで。

run_script
branch_file="${SRCROOT}/path/to/Branch.h"
branch=`git branch | grep '^\*' | cut -b 3-`
echo -n '#define CURRENT_BRANCH @"' > $branch_file
echo -n $branch >> $branch_file
echo '"' >> $branch_file

ただし! スクリプトはコードがコンパイルされる前に実行されないといけないので、ドラッグ&ドロップで必ず Compile Sources フェーズの上に持ってきます。

xcode ss

ついでに .gitignore にも追記しておくとよいでしょう。

.gitignore
path/to/Branch.h

KGStatusBar を導入する

github から KGStatusBar のソースをダウンロードしてプロジェクトに取り込んでおきます。

git clone https://github.com/kevingibbon/KGStatusBar.git

表示用のコードを書く

プロジェクトに Branch.h を追加して、次のようなコードをどこかに書いておきます。

AppDelegate.m
#import "Branch.h"
#import "KGStatusBar.h"

- (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {    
    ...
#ifdef DEBUG
    [[[KGStatusBar alloc] init] showWithStatus: CURRENT_BRANCH barColor: [UIColor grayColor] textColor: [UIColor whiteColor]];
#endif
    ...
}

ついでにバージョン番号も表示する例です。

    NSString *statusString = [NSString stringWithFormat: @"%@ (%@)", CURRENT_BRANCH, [[[NSBundle mainBundle] infoDictionary] objectForKey: @"CFBundleShortVersionString"]];
    [[KGStatusBar sharedView] showWithStatus: statusString barColor: [UIColor grayColor] textColor: [UIColor whiteColor]];

まとめ

  • Run Script フェーズを利用してコンパイル前に最新の Branch.h を作る
  • KGStatusBar を使ってお手軽に表示する
26
25
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
26
25