Swift: Can I change views without clicking on a link.

I want to automatically load different views depending on OS (OSX or iOS). Is there a way that I can do this without the user having to click on a link? This is my code so far.

struct ContentView: View {

#if os(iOS)
    var myOS = "iOS"
#elseif os(OSX)
    var myOS = "OSX"
#else
    var myOS = "Something Else"
#endif
       
    var body: some View {
        
        NavigationStack {
            VStack {
                
                Text("PLEASE WAIT....")
                    .font(.system(size: 24))
                    .fontWeight(.bold)
                
            }
            .padding()
            if (myOS == "OSX"){
                // Goto Screen  for iMac
            }
            else{
                // go to screen for iOS
            }
        }
    }
}

If I use "NavigationLink", my understanding is that the user would need to click on a link. Is there some way to do this without user interaction?

Answered by darkpaw in 788146022

Okay, why do you need a NavigationStack? What's the layout of your app? I have an app that does this check in the main scene:

var body: some Scene {
	WindowGroup {
		if(UIDevice.current.userInterfaceIdiom == .pad) {
			TabletView()

		} else {
			PhoneView()
		}
	}

So, if the device in use is an iPad, the TabletView is shown. Otherwise, the PhoneView is shown.

You can tailor this for your own needs. For example, if you want to do this in your ContentView you could use something like this:

struct ContentView: View {
    var body: some View {
#if os(iOS)
        IOSView()
#elseif os(OSX)
        OSXView()
#else
        FallbackView()
#endif
    }
}

Note that I've removed your myOS var altogether since you don't need it if you do the above.

Accepted Answer

Okay, why do you need a NavigationStack? What's the layout of your app? I have an app that does this check in the main scene:

var body: some Scene {
	WindowGroup {
		if(UIDevice.current.userInterfaceIdiom == .pad) {
			TabletView()

		} else {
			PhoneView()
		}
	}

So, if the device in use is an iPad, the TabletView is shown. Otherwise, the PhoneView is shown.

You can tailor this for your own needs. For example, if you want to do this in your ContentView you could use something like this:

struct ContentView: View {
    var body: some View {
#if os(iOS)
        IOSView()
#elseif os(OSX)
        OSXView()
#else
        FallbackView()
#endif
    }
}

Note that I've removed your myOS var altogether since you don't need it if you do the above.

I would try it differently:

struct ContentView: View {

//#if os(iOS)
//    var myOS = "iOS"
//#elseif os(OSX)
//    var myOS = "OSX"
//#else
//    var myOS = "Something Else"
//#endif
       
    struct ScreenIOS: View {
        
        var body: some View {
            VStack {
                Text("Welcome iOS !")
            }
        }
    }

    struct ScreenMacOS: View {
        
        var body: some View {
            VStack {
                Text("Welcome MacOS !")
            }
        }
    }

    var body: some View {
        
        NavigationStack {
            VStack {
                
                Text("PLEASE WAIT …") // Wait do you wait for ?
                    .font(.system(size: 24))
                    .fontWeight(.bold)
                
            }
            .padding()
            if ProcessInfo.processInfo.isiOSAppOnMac {
//            if (myOS == "OSX") {
                ScreenMacOS()  // Goto Screen  for iMac
            }
            else {
                ScreenIOS()  // go to screen for iOS
            }
        }
    }
}
Swift: Can I change views without clicking on a link.
 
 
Q