-3

I would like to do an iOS detection at the beginning of the app, to load one or another view controller in app delegate, and from there, I would code the main things separately, depending on the iOS version of the device.

The idea is to make two (or three) view controllers at the beginning, one for iOS 9 and another for iOS 8/iOS 7 but nothing lower than that because I'm coding in Swift 2.

I would like to know if that's the good approach to code a universal app compatible with multiple iOS versions.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alfro
  • 1,524
  • 2
  • 21
  • 37
  • Why do you need different view controllers depending on the version of iOS? – rmaddy May 05 '16 at 14:36
  • How different are you planning to make your app based on different version of iOS? It seems like you would stick with one view controller for all the versions and then handle version specific things, like UIAlertController vs UIAlertView, where you need it. – keithbhunter May 05 '16 at 14:37
  • to avoid many if else sentence that make the code unreadable – Alfro May 05 '16 at 14:37
  • 1
    Again, how many iOS version specific things do you plan on doing? There shouldn't be that many. – keithbhunter May 05 '16 at 14:38
  • `if/else` statements for what? You really need to update your question with more specific details. You are being far too vague. – rmaddy May 05 '16 at 14:39
  • @rmaddy if else sentence for different way of doing things depending on the operating system, for example for UIAlertController VS deprecated UIAlertView – Alfro May 05 '16 at 14:40
  • Since you plan to support iOS 7, you can just use `UIAlertView` in your entire app. You don't need to use `UIAlertController` at all. – rmaddy May 05 '16 at 14:42
  • @keithbhunter the problem is I don't know exactly how many cases it will be because I'm new in swift 2. I don't know if there is a lot or not, but I've already found deprecated UIAlertView. With push notifications there is also things. I don't want to start coding a project and find that I have if else everywere... – Alfro May 05 '16 at 14:43
  • @rmaddy there are other cases that will need if else and that's precisely what I want to avoid.. – Alfro May 05 '16 at 14:43
  • I think you will be better off with a couple of if/else statements here and there for those cases than creating 3 copies of very similar view controllers with minor changes between them. There are 2 benefits to this approach: 1. There is way less code, which makes debugging and looking back at old code much much easier. 2. It is easier to tell what features specifically rely on iOS version. All you do is look at the if/elses instead of trying to find the differences between three versions of your code base. – keithbhunter May 05 '16 at 14:48
  • 2
    Remember to put all this in context... [84% of devices run the latest version of iOS](https://developer.apple.com/support/app-store/), and a staggering 95% of devices run either iOS 8 or 9. Supporting older versions of iOS really isn't worth spending a non-trivial amount of time on. – Hamish May 05 '16 at 14:51
  • @originaluser2 that's indeed a useful info. – Alfro May 05 '16 at 14:55
  • @keithbhunter I'm not going to duplicate any code, I expect to reuse all my classes. The only code I expect to write different times is the controllers. – Alfro May 05 '16 at 14:56
  • Another way to think about it is this: how complex will your view controller(s) be? You'll have to evaluate the balance of the amount of code you'll have to write that's **identical** between the view controllers (i.e. non-platform/OS specific) versus how much is different (platform-specific). The more complex your view controller(s) become, the more duplicated code you'll have to maintain/test/etc. In almost all VCs that I can think of that I've personally worked on, ratio would be 100:1 or greater identical behavior vs. platform-specific. Don't lose the forest for the trees. – fullofsquirrels May 05 '16 at 14:57
  • Possible duplicate of [Check OS version in SWIFT](http://stackoverflow.com/questions/24503001/check-os-version-in-swift) – NobodyNada May 05 '16 at 23:03

1 Answers1

3

Systems operating Version can be checked. In swift, the following lines will do the job for the current task:

if #available(iOS 9, *) {

}else if #available(iOS 8, *) {

}else if #available(iOS 7, *) {

}
KrishnaCA
  • 5,615
  • 1
  • 21
  • 31
  • 1
    This is definitely the best approach for checking for specific OS dependent features. Hacking with Swift has a simple example for UIStackView https://www.hackingwithswift.com/new-syntax-swift-2-availability-checking – suite22 May 05 '16 at 17:57