在警报视图操作中创建'Link'



好的,不确定这是否可能,但基本上我有一个长按手势识别器,可以打开警报。我想要的是,当警报出现时,用户会点击按钮,它会在safari中打开一个链接。代码:

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",我希望该按钮能将它们带到链接。如果有人能帮忙,我们将不胜感激。

点击按钮时,可以使用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)

最新更新