Search code examples
iosswiftswift2ios9uialertcontroller

How to make message of UIAlertController copyable in swift2, iOS9?


I've a simple question: How can I make the message of an UIAlertController be selectable and copyable by the user?

The controller is initiated like so:

let alertController = UIAlertController(title: "Hello World", message: "Copy Me!", preferredStyle: .Alert)

and displayed like so:

presentViewController(alertController, animated: true, completion: nil)

Solution

  • Adam's correct that UIAlertController doesn't provide text selection functionality, so a traditional copy/paste solution isn't going to work. You could alternatively provide a button on your UIAlertController that copies a string to the pasteboard.

    UIPasteboard.general.string = "Copy Me!"