LoginSignup
1

More than 5 years have passed since last update.

【Swift 4】テストビルドか判断する方法

Last updated at Posted at 2019-02-22

スキーム毎に処理を分けたい時、
例えばテスト実行中にアクセスするURLを分岐したい時に使えます。

スキームを選択してEdit Scheme
Environment Variblesに適当なKeyとValueをセットする
スクリーンショット 2019-02-22 17.50.17.png

Viewcontroller.swift
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        if Scheme.isTesting() {
            print("Testing")
        } else {
            print("Not Testing")
        }
    }

}

class Scheme {
    static func isTesting() -> Bool {
        if ProcessInfo().environment["isTesting"] == nil {
            return false
        }
        return true
    }
}

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
1