1

I want to display message of alert dialog with clickable link. For example, my message For more info please visit our website: (I WANT TO PUT LINK HERE FOR USER TO INTERACT).

How can I achieve this kind of thing?

This is my code

let dialog = MDCAlertController(title: DialogErrorMessage().errorTitle, message: message)

    dialog.buttonTitleColor = UIColor(red:0.03, green:0.62, blue:0.09, alpha:1.0)

    let okayButton = MDCAlertAction(title: DialogTitleMessage().okayTitle) { (action) in

    }
    dialog.addAction(okayButton)

    dialogPresent(view: dialog)
Visal Sambo
  • 1,270
  • 1
  • 19
  • 33

2 Answers2

1

To make a label clickable in your custom MDCAlertController, you need to do following workaround:

  • Enumerate the labels in the alert controller. (alertController is the name of your MDCAlertController's instance)

    for (UILabel *label in [alertController.view subviewsOfClass:[UILabel class]]) { //Here you get the **label instance** }

  • Add the tap gesture on your label as

    let tap = UITapGestureRecognizer(target: self, action: Selector("tapFunction:")) label.addGestureRecognizer(tap)

  • Set user interaction of your label true

    label.isUserInteractionEnabled = true

The complete code is as below:-

for (UILabel *label in [alertController.view subviewsOfClass:[UILabel class]]) {
        label.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: Selector("tapFunction:"))
        label.addGestureRecognizer(tap)
    }
pkc456
  • 8,350
  • 38
  • 53
  • 109
  • This is no longer needed. New API available for links in message: https://github.com/material-components/material-components-ios/blob/617675f3137259ed0cb0b7ca5875304284d1d2ce/components/Dialogs/src/MDCAlertController.h#L52-L67 – GK100 Jun 05 '20 at 20:29
0

MDCAlertController now supports Links in the message. See: https://github.com/material-components/material-components-ios/blob/617675f3137259ed0cb0b7ca5875304284d1d2ce/components/Dialogs/src/MDCAlertController.h#L52-L67

Old answer (Before May, 2020):

Consider using the accessoryView API to attach a custom view to the dialog, where you can implement the functionality that you need. This view can be added to or completely replace the message section of the dialog, while keeping the title and actions that you instantiated the dialog with.

GK100
  • 3,664
  • 1
  • 26
  • 19