Is it possible to create an alert with a message in it that contains text with a clickable URL, with a further redirect in Safari? Example: https://prntscr.com/lujcx5
Asked
Active
Viewed 4,060 times
3
-
https://stackoverflow.com/questions/34782288/is-it-possible-to-add-a-hyperlink-to-a-uialertcontroller – gavsta707 Dec 13 '18 at 15:48
-
you can always create your own alert-controller lookalike thing. – holex Dec 13 '18 at 15:49
-
This is not possible.For more information prefer : https://stackoverflow.com/questions/39438417/how-can-i-add-a-url-to-an-alert Thank you – Yash Bhikadiya Dec 13 '18 at 15:49
-
Thank you all for your help! – Sasha Tsvigun Dec 13 '18 at 15:53
1 Answers
-2
You may want to try this,
let alert = UIAlertController(title: "Default Title", message: nil, preferredStyle: .alert)
let textView = UITextView()
textView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
let controller = UIViewController()
textView.frame = controller.view.frame
controller.view.addSubview(textView)
textView.backgroundColor = .clear
alert.setValue(controller, forKey: "contentViewController")
let height: NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 150)
alert.view.addConstraint(height)
let attributedString = NSMutableAttributedString(string: "http://movies.com")
let url = URL(string: "http://movies.com")
attributedString.setAttributes([.link: url ?? ""], range: NSMakeRange(0, attributedString.length))
textView.attributedText = attributedString
textView.isUserInteractionEnabled = true
textView.isEditable = false
// Set how links should appear: blue and underlined
textView.linkTextAttributes = [
.foregroundColor: UIColor.blue,
.underlineStyle: NSUnderlineStyle.single.rawValue
]
let okAction = UIAlertAction(title: "OK", style: .default) {
UIAlertAction in
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) {
UIAlertAction in
}
alert.addAction(okAction)
alert.addAction(cancelAction)
present(alert, animated: true, completion: nil)

Bappaditya
- 9,494
- 3
- 20
- 29
-
4This code is dangerous. It accesses private, internal structures that could change in a future iOS update and cause the app to crash. Avoid code like this. – rmaddy Dec 13 '18 at 19:02
-
I'll add that it might causes a app rejection in future if Apple decides so. – Larme Dec 14 '18 at 09:15