UIAlertView/UIAlertController iOS 7 and iOS 8 compatibility

后端 未结 14 1521
暗喜
暗喜 2020-11-28 22:31

I am using Swift to write an app and I need to show an alert. The app must be iOS 7 and iOS 8 compatible. Since UIAlertView has been replaced with UIAlert

相关标签:
14条回答
  • 2020-11-28 23:00

    For non-swift code, pure objective-C do this

    if ([UIAlertController class])
        {
            // use UIAlertController
            UIAlertController *alert= [UIAlertController
                                          alertControllerWithTitle:@"Enter Folder Name"
                                          message:@"Keep it short and sweet"
                                          preferredStyle:UIAlertControllerStyleAlert];
    
            UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * action){
                                                           //Do Some action here
                                                           UITextField *textField = alert.textFields[0];
                                                           NSLog(@"text was %@", textField.text);
    
                                                       }];
            UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction * action) {
    
                                                               NSLog(@"cancel btn");
    
                                                               [alert dismissViewControllerAnimated:YES completion:nil];
    
                                                           }];
    
            [alert addAction:ok];
            [alert addAction:cancel];
    
            [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                textField.placeholder = @"folder name";
                textField.keyboardType = UIKeyboardTypeDefault;
            }];
    
            [self presentViewController:alert animated:YES completion:nil];
    
        }
        else
        {
            // use UIAlertView
            UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter Folder Name"
                                                             message:@"Keep it short and sweet"
                                                            delegate:self
                                                   cancelButtonTitle:@"Cancel"
                                                   otherButtonTitles:@"OK", nil];
    
            dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
            dialog.tag = 400;
            [dialog show];
    
        }
    
    0 讨论(0)
  • 2020-11-28 23:00

    Try below code. It works fine for both iOS 8 and below version.

    if (IS_OS_8_OR_LATER) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction *cancelAction = [UIAlertAction
                                     actionWithTitle:@"OK"
                                     style:UIAlertActionStyleCancel
                                     handler:^(UIAlertAction *action)
                                     {
    
                                     }];
        [alertVC addAction:cancelAction];
    
        [[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{
    
        }];
    }
    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
    
    0 讨论(0)
  • 2020-11-28 23:04

    You can resolve your issue using this code :-

    var device : UIDevice = UIDevice.currentDevice()!;
            var systemVersion = device.systemVersion;
            var iosVerion : Float = systemVersion.bridgeToObjectiveC().floatValue;
            if(iosVerion < 8.0) {
                let alert = UIAlertView()
                alert.title = "Noop"
                alert.message = "Nothing to verify"
                alert.addButtonWithTitle("Click")
                alert.show()
            }else{
                var alert : UIAlertController = UIAlertController(title: "Noop", message: "Nothing to verify", preferredStyle: UIAlertControllerStyle.Alert)
                alert.addAction(UIAlertAction(title: "Click", style:.Default, handler: nil))
                self.presentViewController(alert, animated: true, completion: nil)
            }
    

    and UIKit had to be marked Optional rather than Required.

    Courtsey :- Alert that can work on iOS 7 and iOS 8

    0 讨论(0)
  • 2020-11-28 23:04

    Here to check two way of UIAlertView and UIAlertContoller.

    Check 1 : iOS verstion check UIAlertController Class.

        if #available(iOS 8.0, *) {
    
            // UIALertController
            let alert = UIAlertController(title: "Alert", message: "Alert after 8.0", preferredStyle: .Alert)
            let cancelAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
            alert.addAction(cancelAction)
            presentViewController(alert, animated: true, completion: nil)
        } else {
    
            // UIALertView
            UIAlertView(title: "Alert", message: "Alert below iOS V 8.0", delegate: nil, cancelButtonTitle: "OK").show()
        }
    

    Check 2 : check UIAlertController nil then iOS version below 8.0.

        if objc_getClass("UIAlertController") != nil {
    
            // UIALertController
            let alert = UIAlertController(title: "Alert", message: "Alert after 8.0", preferredStyle: .Alert)
            let cancelAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
            alert.addAction(cancelAction)
            presentViewController(alert, animated: true, completion: nil)
    
        }
        else {
    
            // UIALertView
            UIAlertView(title: "Alert", message: "Alert below iOS V 8.0", delegate: nil, cancelButtonTitle: "OK").show()
        }
    
    0 讨论(0)
  • 2020-11-28 23:09

    If this is shared code, and there is the possibility that the code can be used in an iOS 8 extension (where UIAlertView and UIActionSheet are restricted APIs) as well as iOS 7, where UIAlertController does not exist, have a look at: JVAlertController

    It is an API-compatible back-port of UIAlertController to iOS 7, that I undertook to make SDK code safe for use in both iOS 7 and iOS 8 extensions.

    0 讨论(0)
  • 2020-11-28 23:12

    In iOS8, there is a new class UIAlertController that replaces UIAlertView and UIActionSheet. From iOS8 on, use UIAlertController, and for iOS8 and before use UIAlertView and UIActionSheet. I think that iOS8 added size classes that change UIAlertView display direction. See: https://github.com/wangyangcc/FYAlertManage

    0 讨论(0)
提交回复
热议问题