任务很简单。使用给定的urlString
在有效时将其打开。这是我尝试的:
func openURL(_ urlString: String) {
guard let url = URL(string: urlString) else {
showInvalidUrlAlert()
return
}
UIApplication.shared.open(url)
}
此示例适用于以下示例:“https://www.google.de/?hl=de”但是,当传递无效的URL时,这在我的应用程序中也是可能的(例如:“asdfd”),我在控制台上收到此错误,但在应用程序中什么也没有发生:
[默认]无法打开URL asdf:错误域= NSOSStatusErrorDomain代码= -50“无效的输入参数” UserInfo = {NSDebugDescription =无效的输入参数,_LSLine = 252,_LSFunction =-[_ LSDOpenClient openURL:options:completionHandler:]}
最佳做法是什么?
最佳答案
您可能要使用completionHandler
参数:
func openURL(_ urlString: String) {
guard let url = URL(string: urlString) else {
showInvalidUrlAlert()
return
}
UIApplication.shared.open(url, completionHandler: { success in
if success {
print("opened")
} else {
print("failed")
// showInvalidUrlAlert()
}
})
}
关于ios - 打开时处理错误URL的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63101116/