因此,我正在尝试获得URLSession URLError.code .notConnectedToInternet,但我得到的只是当从互联网断开连接时使用URLSession时的Code(rawValue: -1020)。
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
}
}发布于 2022-02-11 00:20:15
error code -1020的意思是dataNotAllowed。这意味着连接失败,因为目前设备上不允许使用数据。
您试图捕获的notConnectedToInternet error code -1009意味着连接失败,因为设备没有连接到互联网。
所以notConnectedToInternet和dataNotAllowed不一样。在这种情况下,请检查设备设置。
https://stackoverflow.com/questions/71073061
复制相似问题