Friday, October 28, 2016

Simple About Box in Swift

Here is a really simple example of how to create an about box from a UIAlertController in iOS using Swift. The box displays the application icon, the current version and some text about the application:

        let alertMessage = UIAlertController(title: "Camping Directory", message: "", preferredStyle: .alert)
        // Application Icon
        let image = UIImage(named: "AppIcon40x40")
        let imageView = UIImageView(frame: CGRect(x: 115, y: 48, width: 40, height: 40))
        imageView.image = image
        imageView.layer.cornerRadius = 8.0
        imageView.clipsToBounds = true
        // Application Version
        let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
        // Description
        var message = "\n\n\nVersion " + version! + "\n\n"
        message += "This program is..."
        alertMessage.message = message
        let action = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertMessage .addAction(action)
        self.present(alertMessage, animated: true, completion: nil)