【问题标题】:With Swift, is there a way to open URL and get the http error使用 Swift,有没有办法打开 URL 并获取 http 错误
【发布时间】:2020-05-06 14:18:04
【问题描述】:

我在做

if !UIApplication.shared.openURL(url) {         
    print("Error")
}

问题是我在控制台中出现消息错误,我正在寻找如何知道引发了哪些特定 HTTP 错误。

【问题讨论】:

  • 尝试使用URLSession 对其进行请求,而不是在Safari.app 中打开它?
  • UIApplication.shared.canOpenURL(url) 检查您的网址是否可以打开或您的网址是否有效。

标签: swift http-error openurl


【解决方案1】:

你可以通过这样的方式得到你的回应。

let URL = URL(string: "www.google.com")
let task = URLSession.shared.dataTask(with:url!) { _, response, _ in 
     if let status = response as? HTTPURLResponse {
          print(status.statusCode)
     }
}

task.resume()

对任何语法问题表示歉意。目前没有可用的 XCode。

【讨论】:

    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 2020-08-13
    • 2022-06-18
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 2011-08-25
    相关资源
    最近更新 更多