Как я могу добавить URL в оповещение?

Есть ли способ добавить URL для пользователей, которые ищут дополнительную информацию? Мое оповещение выглядит так

let alert = UIAlertController(title: "Alert details", message: "For more detailed information, click the link below", preferredStyle: UIAlertControllerStyle.Alert)
    // add url here
    let okayAction = UIAlertAction(title: "Ok", style: .Default) { (action) in
        print(action)
    }
    alert.addAction(okayAction)

Идея состоит в том, чтобы перенаправить их на веб-страницу и закрыть UIAlertController в приложении.

2 ответа

Решение

Вы не можете добавить произвольный интерфейс к UIAlertController. Заголовок и сообщение не могут быть изменены. Вы не можете добавить дополнительный текст. Вместо этого добавьте еще одну кнопку (UIAlertAction), которая направляет их на веб-страницу. Либо так, либо используйте какой-то другой интерфейс вместо UIAlertController (например, вы можете установить представленный контроллер представления, который выглядит как предупреждение).

      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)

отсюда: https://overcoder.net/q/1502030/кликабельный-url-на-uialertcontroller

Другие вопросы по тегам