Home > AI > IOS > SwiftUI >

First Launch View

FirstLaunchView.swift

struct FirstLaunchView: View {
    @Environment(\.presentationMode) private var mode

    var body: some View {
        Text("FirstLaunchView")
            .background(Color.blue)
            .onTapGesture {
                let generator = UINotificationFeedbackGenerator()
                generator.notificationOccurred(.success)
                self.mode.wrappedValue.dismiss()
                UserDefaults.standard.setValue(false, forKey: UserDefaultsKeyEnum.isFirstLaunch.rawValue)
            }
    }
}

FirstLaunchViewModifier.swift

struct FirstLaunchViewModifier: ViewModifier {

    @State var isFirstLaunch: Bool = UserDefaults.standard.value(forKey: UserDefaultsKeyEnum.isFirstLaunch.rawValue) as? Bool ?? true 
    func body(content: Content) -> some View {
      
        content
            .fullScreenCover(isPresented: $isFirstLaunch) {
                FirstLaunchView()
            }

    }
}

extension View {
    func firstLaunchViewModifier() -> some View {
        self.modifier(FirstLaunchViewModifier())
    }
}

Main.swift

@main
struct ddApp: App {
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(HomeGlobal())
                .firstLaunchViewModifier()
        }
    }
}

Leave a Reply