After iOS 8, can I continue to use UIActionSheet a

2019-06-17 17:58发布

问题:

I have apps which use UIActionSheet and UIAlertView.
In iOS8, Apple's documentation and some websites say that they are deprecated in iOS8.

UIActionSheet documentation
Important: UIActionSheet is deprecated in iOS 8. (Note that UIActionSheetDelegate is also deprecated.) To create and manage action sheets in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet.

But in Xcode 6 with deployment target 8.0 does not generate warning for the use of UIActionSheet and UIAlertView.
Normally Xcode generates warning for deprecated API.
Why doesn't Xcode generate warnings for UIActionSheet and UIAlertView?
Does this mean Apple does not actually deprecate those classes?
It is very dangerous if they actually deprecated them and Xcode does not generate warnings.


In another apple's documentation "What's New in iOS 8.0" says:

The new UIAlertController class replaces the UIActionSheet and UIAlertViewclasses as the preferred way to display alerts in your app.

https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html

And UIActionSheet and UIAlertView is not listed on Deprecated APIs section of the documentation of the above URL.
So, this documentation means UIAlertController is preferable but UIActionSheet and UIAlertView are also still available in iOS 8.

Should I replace UIActionSheet and UIAlertView in my apps with new UIAlertController class?
Or can I continue to use UIActionSheet and UIAlertView?

回答1:

Yes you should. Preferable leads to deprecated which leads to being cut off suddenly.



回答2:

It's always best to not use depreciated code, it all adds up for well written code. So yeah, use UIAlertController.



回答3:

Yes, you should replace your code.

When I tried to use in my code some of the functions and delegate methods for both of the Classes UIActionSheet and UIAlertView were not working.

I was getting issues and weird results each time.

Therefore, you should not use deprecated APIs.

I am sure about this, I think if the app is uploaded to App Store with deprecated APIs then that App can be rejected.



回答4:

It takes a while to figure out the new method, so you might find this code useful. I’m supporting older versions of iOS so I use a conditional to decide which to use. This code runs in my app delegate. If you are running it in a view controller, replace

[self.window.rootViewController presentViewController:alert animated:true completion:nil];

with

[self presentViewController:alert animated:YES completion:nil];

The #define is in my .pch but I put it here since it is what I use in the conditional.

 #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

  - (void)applicationWillEnterForeground:(UIApplication *)application {

        if (self.window.rootViewController) {

            if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
                [self displayUIAlertController];
            } else {
                [self displayUIAlertView];
            }
        }

    }

    - (void)displayUIAlertController {

        NSString *alertMessage = [NSString stringWithFormat:@"Do you want to resume playing %@ or start a new session?", GAME_NAME_TITLE];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Welcome Back"
                                                                       message:alertMessage
                                                                preferredStyle:UIAlertControllerStyleAlert];

        // You can add as many actions as you want
        UIAlertAction *startNewSession = [UIAlertAction actionWithTitle:@"Start New Session"
                                                                  style:UIAlertActionStyleDefault
                                                                handler:^(UIAlertAction *action) {
           [self startNewSession];
        }];

        UIAlertAction *doNothingAction = [UIAlertAction actionWithTitle:@"Resume"
                                                                  style:UIAlertActionStyleDefault
                                                                handler:^(UIAlertAction *action) {
                        // Do nothing
        }];

        // Add actions to the controller so they will appear
        [alert addAction:doNothingAction];
        [alert addAction:startNewSession];

        // Finally present the action
        [self.window.rootViewController presentViewController:alert animated:true completion:nil];
    }

    - (void)displayUIAlertView {
        NSString *messageWithTitle = [NSString stringWithFormat:@"Do you want to resume playing %@ or start a new session?", GAME_NAME_TITLE];
        self.alertView = [[UIAlertView alloc] initWithTitle:@"Welcome Back"
                                                        message:messageWithTitle
                                                       delegate:self
                                              cancelButtonTitle:@"Resume"
                                              otherButtonTitles: @"Start New Session",nil];
        [self.alertView show];
    }

    #pragma mark - Alert on restart
    // buttonIndex 0 is cancel and the game continues
    // buttonIndex 1 is Start New Session and the old results are saved and new session started
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (buttonIndex == 1) {
            [self startNewSession];
        }
    }


回答5:

Deprecated code typically means that the prior versions are not supported from the version marked and in the future. If you were building a legacy app (or an app that will be supporting prior versions) one should use the UIActionSheet and UIAlertView, however if you are updating code for iOS8+ you should use UIAlertController. The nice thing is that the code you wrote previously will not be affected so users can use a legacy version of the app without problem.



回答6:

You should be able to continue using UIAlertView and UIActionSheet in iOS8 since they were deprecated in iOS8 and not in iOS7. That said, I've run into similar problems => UIAlertView automatic newline gone in iOS8?. So it appears that we may need to move to UIAlertController faster than we anticipated.