0

I am programming an App with Swift, and targeting iOS 7.1 as the lowest, and I noticed that UIAlertView is deprecated in iOS 8.

So does this mean I should ONLY use UIAlertView to display an alert in my app?

or should I use some code that checks which version of iOS is running, and depending on the version of iOS running, use UIAlertController for iOS 8, and then use UIAlertView if they're running iOS 7?

Edit: I should add, that UIAlertView does work on both iOS 7 and 8, but I am just wondering if I should use JUST UIAlertView for both, or have a check and run UIAlertController instead if they're on iOS 8. I know its best practice to use the new API since the other is deprecated, so I am curious if it is an issue to just use UIAlertView across both

Jake Weso
  • 555
  • 1
  • 6
  • 14
  • Try it. What happens? – rmaddy Oct 17 '14 at 18:23
  • Well UIAlertView works on both versions, but I'm curious if it is an issue to use it for both, or if I should have a check for which version the app is running on, then using UIAlertController if they're on iOS 8 and UIAlertView if they're on iOS 7. Just wondering what the best practice would be. It isn't too much work to have a check and run different code for now, and I wouldn't mind it over just using UIAlertView, as I would prefer the newer API being used on iOS 8 – Jake Weso Oct 17 '14 at 18:28

5 Answers5

4

You should do it either way. It doesn't matter as long as it works.

If your app supports multiple iOS versions and a certain API is only deprecated is some of the app supported iOS versions then there no problem using just the older API (again, assuming it still works with the newer iOS).

Think of this way - you have two options:

  1. Use just UIAlertView for now. It works as your app supports both iOS 7 and 8. Someday, maybe next year, you decided to drop support for iOS 7. At that time you can replace all uses of UIAlertView with UIAlertController. It's a onetime hit to do the change.
  2. Update your code like in the other answers. Write code that checks now if UIAlertController is available. Use if it is, else use UIAlertView. Then next year you drop iOS 7 support and then you update all of the existing code to remove the check for UIAlertController and remove the code to use UIAlertView. So you add lots of code now and make a bunch of changes later. That's twice the work of option 1 for no gain.
rmaddy
  • 314,917
  • 42
  • 532
  • 579
1

You should use new APIs wherever possible as deprecated methods could eventually be unavailable and/or not produce the results you might expect. You should do a check to see if UIAlertController is available at run-time and then use it when you can.

Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
  • 2
    With extremely rare exception, no deprecated API has actually disappeared. In a case like this, the developer has two choices: 1) Use `UIAlertView` and not worry about it for now. When support for iOS 7 is dropped from the app, a switch to use `UIAlertController` can be made. 2) Update the app now to use one or the other depending on which is available. When support for iOS 7 is dropped from the app, remove the use of `UIAlertView`. Option 1 is probably less total work. – rmaddy Oct 17 '14 at 18:26
  • Option 1 is also more likely to cause subtle bugs because the deprecated APIs no longer work exactly as expected – Bradley Thomas Mar 15 '16 at 14:01
1

You should consider the unique requirements of your app. For the most part, I agree with rmaddy. You should default to just using UIAlertView until you deprecate iOS 7 support in your app. However, take some time to consider how you're currently using UIAlertView and whether you are able to give iOS 8 users a better experience by supporting UIAlertController.

They're not identical in functionality. For example, UIAlertController supports more than 2 text fields, and alerts with destructive buttons (even more than 1).

One example of a good place for UIAlertController is the Facebook app, when canceling a post:

enter image description here

This use case could benefit from a red destructive button for the "Delete" command, which is only available in iOS 8. If you're using UIAlertView for something like this, consider using UIAlertController on iOS 8.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Thanks! Very useful information. Fortunately the alerts we need are very simple in the app, so it isn't an issue, but that's good to know for our other projects :) – Jake Weso Oct 17 '14 at 19:15
0

I am highly recommend you to use all new features of the iOS API.

To detect if it is possible to run old alert screen use this code snipet:

if let gotModernAlert: AnyClass = NSClassFromString("UIAlertController") {

            println("UIAlertController can be instantiated")

            //make and use a UIAlertController

        }
        else {

            println("UIAlertController can NOT be instantiated")

            //make and use a UIAlertView
        }
}
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
  • 1
    Why do you make this recommendation? An app that supports iOS 7 and 8 can safely use just `UIAlertView` now. Why add all of these unneeded code changes now? In the future, when iOS 7 support is removed from the app, a switch to `UIAlertController` can be made. Non need for all of the extra overhead of checking which is available at runtime. – rmaddy Oct 17 '14 at 18:28
  • deprecated methods could be considerate as point to reject app during review. – Oleg Gordiichuk Oct 17 '14 at 18:29
  • 1
    That is absolutely false. Private APIs get you rejected, never deprecated methods. – rmaddy Oct 17 '14 at 18:31
  • Two days ago app was rejected due to 2 deprecated methods. – Oleg Gordiichuk Oct 17 '14 at 18:35
  • 1
    I highly doubt that was the reason. The answer to your question is using deprecated APIs. Your app wouldn't be rejected for it. – rmaddy Oct 17 '14 at 18:37
  • I am sharing my experience it was connected with API that was deprecated in very old iOS version. I was using ASIHTTP library – Oleg Gordiichuk Oct 17 '14 at 18:39
  • 1
    What was the deprecated method? `UIDevice uniqueIdentifier` is probably the only case (it wasn't just deprecated, it was made illegal - a very rare exception). – rmaddy Oct 17 '14 at 18:43
  • Yeas it seams to me it was right this case.Tx for explanation of this stuff. – Oleg Gordiichuk Oct 17 '14 at 18:51
0

I'm inclined to agree with rmaddy. I'm faced with the same problem, only difference is that it is an existing app that I'm now making ready to also support iOS8. I've found that UIAlertView is NOT fully foreword compatible in iOS8 - see my SO question: UIAlertView automatic newline gone in iOS8?

Community
  • 1
  • 1
MickeDG
  • 404
  • 5
  • 18