3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSのターゲットバージョンを保護する

Last updated at Posted at 2019-04-23

iOSのバージョンは、簡単に上げられる上に問題なく動作してしまう場合もあり、コードレビューでも人間の誤りを検知しにくいです。
ヒューマンエラーで重大な損害をもたらす割に、守るための方法がないなぁと思っていくつか方法を試しました。

実行時に保護する

この関数はAppDelegate.swiftのトップレベルに記述しています。

AppDelegate.swift
/// 意図しないミスでiOSのバージョンが変わってしまった場合は、実行時にアプリを強制終了させて検知できる。
/// - Attention : このターゲットは、iOS11.0が最低限バージョンになっているのが正しい
func protectDeploymentTarget() {
    guard __IPHONE_OS_VERSION_MIN_REQUIRED == __IPHONE_11_0 else {
        assertionFailure("Deployment target is wrong. you may miss operation or forget to fix above line.")
        return
    }
    print("No problem. You are right.")
}

1. __IPHONE_OS_VERSION_MIN_REQUIRED とは?

Deployment Targetを設定していると、コンパイラが、下限のiOSバージョンを設定してくれます。

2. __IPHONE_11_0 とは?

__IPHONE_OS_VERSION_MIN_REQUIRED で取得できる値は、Int32110000 などの数値のため(iOS11の場合)、__IPHONE_OS_VERSION_MIN_REQUIRED == 11 のような指定をすると常に実行に失敗します。などでシステム定義のバージョン数値を使います。

ビルド時に保護する

ビルド時にターゲットバージョンを保護できます。こちらの方がより確実で、余計なソースをプロダクションコードに乗せなくせスマートです。

check.sh
if [ $IPHONEOS_DEPLOYMENT_TARGET != '11.0' ]; then
exit "iOS Deployment Target should be 11.0!"; exit 1
fi

こちら、 @lovee さんより教えていただきました!ありがとうございます!

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?