Tanvirgeek
Tanvirgeek

Reputation: 712

Show some view, only if iOS 14, SwiftUI

I want to show a Text view say:

Text("Welcome to iOS 14")

if the user is using iOS 14.

If the user is using other iOS versions I Want to show a different Text view, say:

Text("Welcome to Different iOS version") 

How can I achieve that?

Upvotes: 2

Views: 659

Answers (2)

Joannes
Joannes

Reputation: 2940

This also looks at versions 14.1, 14.2 etc...

Return a string and you can add as many houses as you want. You can apply the concept to anything.

struct ContentView: View {  
  
    public var systemVersion: String? {
        return UIDevice.current.systemVersion // "14.2" in my case
    }
    
    var getMessageSystem: String {
        guard let versionOS = Double(systemVersion!)
            else { return "Not a valid iOS version" }
        switch versionOS {
            case _ where versionOS > 14: return "welcome to iOS 14"
            case _ where versionOS > 13: return "welcome to iOS 13"
            case _ where versionOS > 12: return "welcome to iOS 12"
            default: return "welcome"
        }
    }
    
    
    var body: some View {
        Text(getMessageSystem) // welcome to iOS 14
    }
}

Upvotes: 1

YodagamaHeshan
YodagamaHeshan

Reputation: 6500

struct ContentView: View {
    
    var systemVersion = UIDevice.current.systemVersion

    var body: some View {
        if systemVersion.starts(with: "14" ) {
            Text("welcome to ios 14")
        } else {
            Text("Welcome to Different iOS version")
        }
        
    }
}

Upvotes: 1

Related Questions