Skip to content

Instantly share code, notes, and snippets.

@tunds
Created May 5, 2022 09:26
Show Gist options
  • Save tunds/e0875bf3affc14f03ebd299f5e0ff825 to your computer and use it in GitHub Desktop.
Save tunds/e0875bf3affc14f03ebd299f5e0ff825 to your computer and use it in GitHub Desktop.
Simple Example Appstorage and presenting sheet
//
// ContentView.swift
// SimpleAppStorageOnboarding
//
// Created by Tunde Adegoroye on 05/05/2022.
//
import SwiftUI
struct ContentView: View {
// 1. Setup Appstorage to save value as to whether to show welcome screen or not, this is saved in user defaults on the phone and will be used between opening the app if we should show the welcome screen or not
@AppStorage("showWelcome") private var showWelcome: Bool = true
var body: some View {
ZStack {
Color.blue.ignoresSafeArea()
Text("Home view")
.bold()
}
.foregroundColor(.white)
// 2. Use Sheet to present the welcome screen with our view ontop
.sheet(isPresented: $showWelcome) {
VStack {
Text("Welcome view")
.bold()
// 3. Give user the ability to dismiss the screen with the button, and also set the value in user defaults to false so we don't see this appear again after another app launch, unless you delete the app and reinstall it
Button("Tap me to dismiss") {
showWelcome = false
}
}
// 4. Give user the alternative to dismiss the screen with the drag down, and also set the value in user defaults to false so we don't see this appear again after another app launch, unless you delete the app and reinstall it
.onDisappear {
showWelcome = false
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment