UIAlertView is still available in Swift, it's only deprecated in iOS 7. If you want to use the latest iOS 8 API I would recommend doing something like below. Although for simplicity if you're targeting iOS 7 and iOS 8 I would recommend using the standard UIAlertView only and not implementing the UIAlertController.
import UIKit
class ViewController: UIViewController, UIAlertViewDelegate {
let iosVersion = NSString(string: UIDevice.currentDevice().systemVersion).doubleValue
// MARK: - IBActions
@IBAction func showAlertTapped(sender: AnyObject) {
showAlert()
}
// MARK: - Internal
func showAlert() {
if iosVersion >= 8 {
var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
// The order in which we add the buttons matters.
// Add the Cancel button first to match the iOS 7 default style,
// where the cancel button is at index 0.
alert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in
self.handelCancel()
}))
alert.addAction(UIAlertAction(title: "Confirm", style: .Default, handler: { (action: UIAlertAction!) in
self.handelConfirm()
}))
presentViewController(alert, animated: true, completion: nil)
} else {
var alert = UIAlertView(title: "Title", message: "Message", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Confrim")
alert.show()
}
}
func handelConfirm() {
println("Confirm tapped")
// Your code
}
func handelCancel() {
println("Cancel tapped")
// Your code
}
// MARK: - UIAlertViewDelegate
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
if buttonIndex == 0 {
handelCancel()
} else {
handelConfirm()
}
}
}