0
0
iOS Swiftmobile~20 mins

Build configurations (Debug, Release) in iOS Swift - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Build Configurations Demo
This screen shows a label that changes text depending on the build configuration: Debug or Release. It helps understand how to use different settings for Debug and Release builds in an iOS app.
Target UI
-------------------------
| Build Configurations   |
|-----------------------|
| Current Mode: DEBUG    |
|                       |
| [Button: Show Mode]   |
-------------------------
Display a label that shows 'Current Mode: DEBUG' or 'Current Mode: RELEASE' depending on the build configuration.
Add a button labeled 'Show Mode' that when tapped, updates the label with the current build mode.
Use Swift conditional compilation flags (#if DEBUG) to determine the build mode.
The UI should be simple with a label and a button centered vertically and horizontally.
Starter Code
iOS Swift
import SwiftUI

struct ContentView: View {
    @State private var modeText = "Current Mode: UNKNOWN"

    var body: some View {
        VStack(spacing: 20) {
            Text(modeText)
                .font(.title)
                .padding()

            Button("Show Mode") {
                // TODO: Update modeText based on build configuration
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color(UIColor.systemBackground))
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Task 1
Solution
iOS Swift
import SwiftUI

struct ContentView: View {
    @State private var modeText = "Current Mode: UNKNOWN"

    var body: some View {
        VStack(spacing: 20) {
            Text(modeText)
                .font(.title)
                .padding()

            Button("Show Mode") {
                #if DEBUG
                modeText = "Current Mode: DEBUG"
                #else
                modeText = "Current Mode: RELEASE"
                #endif
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color(UIColor.systemBackground))
    }
}

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

This app uses SwiftUI to create a simple screen with a label and a button.

The label shows the current build mode, which is updated when the user taps the button.

Inside the button's action, we use Swift's conditional compilation flags #if DEBUG and #else to check if the app is running in Debug or Release mode.

Depending on the build configuration, the label text updates accordingly.

This helps beginners see how to use build configurations to change app behavior.

Final Result
Completed Screen
-------------------------
| Build Configurations   |
|-----------------------|
| Current Mode: DEBUG    |
|                       |
| [Button: Show Mode]   |
-------------------------
When the user taps the 'Show Mode' button, the label updates to show 'Current Mode: DEBUG' if running a Debug build.
If running a Release build, the label updates to 'Current Mode: RELEASE'.
Stretch Goal
Add a toggle to automatically show the build mode label on app launch without pressing the button.
💡 Hint
Use the .onAppear modifier to set the modeText when the view appears.