UIAlertView/UIAlertController iOS 7 and iOS 8 compatibility

后端 未结 14 1524
暗喜
暗喜 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: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

提交回复
热议问题