在AlertView操作中创建“链接”

问题描述 投票:0回答:1

好吧,不确定这是否可行,但是基本上我有一个长按手势识别器,它会发出警报。我想要的是当警报出现时,用户将单击该按钮,它将在野生动物园中打开一个链接。代码:

        if sender.state == .began
        {
            let alertController = UIAlertController(title: nil, message:
                "Open Product in Safari", preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "Go to Safari", style: .default,handler: nil))

            present(alertController, animated: true, completion: nil)
        }
    }

如您所见,该按钮将显示“转到Safari”,我希望该按钮将其带到链接。如果有人可以帮助,将不胜感激。

ios swift xcode uialertview
1个回答
0
投票

点击按钮时,您可以使用handler闭包执行操作。以下是一个示例:

let alertController = UIAlertController(title: nil, message:
            "Open Product in Safari", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Go to Safari",
                                                style: .default,
                                                handler: { action in
                                                    UIApplication.shared.open(URL(string: "www.mylinktobeopenedinsafari.com")!, options: [:]) { _ in
                                                        print("Link opened")
                                                    }
        }))

present(alertController, animated: true, completion: nil)
© www.soinside.com 2019 - 2024. All rights reserved.