【问题标题】:NSURLConnection finished with error - code -1002 fix does not workNSURLConnection 完成错误 - 代码 -1002 修复不起作用
【发布时间】:2018-08-06 01:34:28
【问题描述】:

我收到此错误 NSURLConnection 已完成错误 - 代码 -1002。 我已将以下代码添加到我的 info.plist 中。有谁知道为什么?

提前致谢

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionMinimumTLSVersion</key>
    <string>TLSv1.0</string>
</dict>

【问题讨论】:

  • 请在您的问题中包含导致错误的实际代码。
  • 如果您能发布更多详细信息,例如 url,那就太好了
  • 状态 NSURLConnection 以错误完成 - 根据文档代码 -1002 请检查。您的 Internet 连接,
  • 检查以下答案
  • 我得到的完整输出是来自控制台的 2018-02-26 15:02:58.691626+0000 hello[4367:4078481] [MC] 延迟加载 NSBundle MobileCoreServices.framework 2018-02-26 15:02:58.694082+0000 hello[4367:4078481] [MC] 加载 MobileCoreServices.framework 2018-02-26 15:02:59.028870+0000 hello[4367:4078549] NSURLConnection 完成错误 - 代码 -1002 report.pdf I是在 iPad 上运行并打开 wifi 吗?

标签: ios iphone swift


【解决方案1】:

我认为这是关于应用程序传输安全性。因为您的网址不是 https。尝试在 info.plist 文件中进行这样的更改

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

您可以在以下链接中查看所有错误代码及其含义

NSError by NSHipster

【讨论】:

  • 嗨,我也试过了。我得到的完整输出是来自控制台 2018-02-26 15:02:58.691626+0000 hello[4367:4078481] [MC] 延迟加载 NSBundle MobileCoreServices.framework 2018-02-26 15:02:58.694082+0000 hello[4367:4078481] [MC] 加载 MobileCoreServices.framework 2018-02-26 15:02:59.028870+0000 hello[4367:4078549] NSURLConnection 完成错误- 代码-1002 report.pdf
【解决方案2】:

错误状态 - 文档中的代码 -1002 请检查。 ,

https://developer.apple.com/documentation/foundation/1508628-url_loading_system_error_codes/nsurlerrorunsupportedurl?language=objc

请用邮递员再次检查网址。

【讨论】:

  • 嗨,我也试过了。我得到的完整输出是来自控制台 2018-02-26 15:02:58.691626+0000 hello[4367:4078481] [MC] 延迟加载 NSBundle MobileCoreServices.framework 2018-02-26 15:02:58.694082+0000 hello[4367:4078481] [MC] 加载 MobileCoreServices.framework 2018-02-26 15:02:59.028870+0000 hello[4367:4078549] NSURLConnection 完成错误- 代码-1002 report.pdf
  • 我已经修改了我的答案,请检查一下
【解决方案3】:

我有同样的问题,但我的情况不同。以下是产生错误的代码:

    if let url = URL(string: imageUrl){
        getDataFromUrl(url: url) { data, response, error in
            DispatchQueue.main.async() {
                if let imageFile = UIImage(data: data) {
                    self.createItem(forModule: module, title: title, description: description, date: date, image: imageFile, table: table)
                } else {
                    self.createItem(forModule: module, title: title, description: description, date: date, image: nil, table: table)
                }
            }
        }
    } else { print("invalid url") }

private func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
    URLSession.shared.dataTask(with: url) { data, response, error in
        completion(data, response, error)
        }.resume()
}

后来我发现 imageUrl 实际上根本不是一个 url 它只是一个字符串,字面意思是“假”就是这样。更糟糕的是它没有被两个 else 语句捕获。

所以我通过添加下面的守卫来解决它:

if let url = URL(string: imageUrl){
        getDataFromUrl(url: url) { data, response, error in

            guard let data = data, error == nil else {
                self.createItem(forModule: module, title: title, description: description, date: date, image: nil, table: table)
                return
            }

            DispatchQueue.main.async() {
                if let imageFile = UIImage(data: data) {
                    self.createItem(forModule: module, title: title, description: description, date: date, image: imageFile, table: table)
                } else {
                    self.createItem(forModule: module, title: title, description: description, date: date, image: nil, table: table)
                }
            }
        }
    } 

现在我可以调用 createItem 函数并成功保存没有图像的项目。 因此,如果您从 API 获取 URL,请特别仔细检查它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 2019-08-30
    • 2018-03-26
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    • 2018-05-10
    相关资源
    最近更新 更多