【发布时间】:2019-04-23 17:30:31
【问题描述】:
当我的应用无法打开 URL 时,我想通过 UIAlertController 向用户显示警报。这是我的代码:
guard let url = URL(string: urlLink) else {
return
}
UIApplication.shared.open(url, options: [:])
还有我创建的警报:
let alert = UIAlertController(title: "Warning", message: "Problem with URL.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(alert, animated: true)
如果我将警报移动到 guard 语句中,它永远不会发生。我通过将urlLink 更改为一些随机的String 来测试它,例如"123"。关于如何显示警报的任何想法?
编辑:
我使用了canOpenURL,它返回Bool。现在我的代码是:
guard let url = URL(string: urlLink) else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:])
} else {
let alert = UIAlertController(title: "Warning", message: "Problem with URL.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(alert, animated: true)
}
【问题讨论】:
标签: ios swift uialertcontroller guard