16
if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)])
     {[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];} //post-iOS6.0
else {[self dismissModalViewControllerAnimated:YES];} //pre-iOS6.0

I'm doing the responds to selector (above) code to handle deprecated methods. That way my app is compatible with older versions of iOS, but I'm getting warnings in my code stating: "'dismissModalViewControllerAnimated:' is deprecated: first deprecated in iOS 6.0" I personally don't like any warning in my code, but more importantly, I read somewhere that apple will complain about warnings in your code.

1) Will Apple complain about warnings in your code?

2) Am I handling deprecated methods correctly?

3) Is there way to turn deprecated method method warnings off?

4
  • 2
    BTW - Apple will not complain about warnings in your code. They have no way to know there are any (if there are any) unless you send them a note and tell them.
    – rmaddy
    Sep 1, 2013 at 1:52
  • Good answer, but my question is more of a general question of what to do in a situation similar to this? I want to support at least 1 or two versions, so in this scenario you are correct, but how about other scenarios where you would need to support both? how would you handle it? Like I stated above and leave the warnings in? Sep 1, 2013 at 1:53
  • 2
    You will not get any deprecation warnings as long as the method wasn't deprecated before your selected "Deployment Target". If the method was deprecated before your "Deployment Target", then you shouldn't make any attempt at all to use the deprecated method. So there is really little to deal with actually.
    – rmaddy
    Sep 1, 2013 at 2:01
  • @rmaddy +1 Agreed, but if the class was written with re-use in mind, you might not know what the deployment target was going to be in advance. Having said that, I might be included to do #if logic based upon __IPHONE_OS_VERSION_MIN_REQUIRED in that scenario, rather than suppressing warnings.
    – Rob
    Sep 1, 2013 at 2:50

3 Answers 3

20
  1. Apple is unaware of any compile-time warnings you receive with your code.

  2. Yes, you are handling this practice correctly. Clearly, in this case, you only need to go through this effort if you're supporting iOS prior to 5.0. But, in general, the technique for testing whether a method can be invoked and then calling the appropriate rendition is absolutely correct.

  3. If you want to turn off the warning, you would simply temporarily suppress the warning and then turn it back on afterwards using the appropriate #pragma syntax:

    if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)])
    {
        //post-iOS6.0
        [self dismissViewControllerAnimated:YES completion:nil];
    } 
    else
    {
        // pre-iOS6.0
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
        [self dismissModalViewControllerAnimated:YES];
    #pragma clang diagnostic pop
    }
    

    By the way, if you want to know what the -W code is for your particular warning, go to your Log Navigator, select the recent build that included the warning, and expand the log, and you'll see it there:

    enter image description here

Also note that while you could suppress the warning like I've illustrated above, in practice, you rarely need to do so. Using your example, if your project's iOS deployment target was 4.3, you wouldn't get the warning. And if your deployment target was 6.0 or higher, you'd get that warning, but then again, you probably wouldn't need this conditional code to call dismissModalViewControllerAnimated because effective iOS 5.0, you can always use dismissViewControllerAnimated.

The only time that you'd need to suppress this warning in your code is if you have source code, to be included in projects in the future, for which you don't know what the deployment target will be. Using your example, if you don't know whether the above code will be included in a project with a 4.3 deployment target or a 5.0+ deployment target. In that case, this syntax is quite useful. But, then again, I you could also use conditional checks on __IPHONE_OS_VERSION_MIN_REQUIRED, e.g.:

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0
    if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)])
    {
        //post-iOS5.0
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        // pre-iOS5.0
        [self dismissModalViewControllerAnimated:YES];
    }
#else
    [self dismissViewControllerAnimated:YES completion:nil];
#endif
1
  • Any thoughts on how one might deal with #pragma commands fro Swift. I am thinking about just defining some constants in a bridging header file and attempting to call that in Swift.
    – Tommie C.
    Feb 6, 2015 at 0:29
1
  1. No
  2. You should use the most recent methods unless you are specifically trying to support old iOS versions, then you should use the method you outlined above. "A method identified as deprecated has been superseded and may become unsupported in the future."
  3. If you change the deployment target in your application target to 5.0, the deprecated warnings for iOS 5 not will be show as errors.

If you are really interested in backwards compatibility, there is a great tutorial by Ray Wenderlich here

1
  • Thanjs for the tutorial link. Sep 3, 2013 at 13:57
0

Yes Many warnings are of present and dismiss view modally to fix that Replace:

in ios 6 these are:-

  1. [self dismissViewControllerAnimated:NO completion:nil];

  2. [self presentViewController:vc animated:NO completion:nil];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.