未检索 .notConnectedToInternet URLSession

Not retrieving .notConnectedToInternet URLSession

所以我正在尝试获取 URLSession URLError.code .notConnectedToInternet 但我得到的只是 Code(rawValue: -1020) 在与互联网断开连接时使用 URLSession。

URLSession.shared.dataTask(with: url) { data, _, error in
       if let error = error {
          print("error.code \((error as! URLError).code)")
          //prints - error.code Code(rawValue: -1020)
            
            if (error as! URLError).code == .notConnectedToInternet {
                print("no internet")
                //doesn't print
                return
            }
        }

error code -1020表示dataNotAllowed。这意味着连接失败,因为设备当前不允许使用数据。

您要捕获的内容 notConnectedToInternet error code -1009 表示连接失败,因为设备未连接到互联网。

因此 notConnectedToInternetdataNotAllowed 不同。在这种情况下,请检查您的设备设置。