6
3

More than 3 years have passed since last update.

SwiftUIでpreview modeかどうかの判定方法

Last updated at Posted at 2020-07-21

SwiftUIでソースを書いてると、
サーバー通信ができないなどで右側のCanvas(Preview)が表示できなくなるときに
以下のif文が便利。

if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"{

実際はこんな感じで利用しました。↓

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")

        .onAppear(){
            if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] != "1"{
                //サーバー通信の処理
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

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