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?
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.