Home > Blockchain >  Not retrieving .notConnectedToInternet URLSession
Not retrieving .notConnectedToInternet URLSession

Time:02-11

So I'm trying to get URLSession URLError.code .notConnectedToInternet but all I'm getting is Code(rawValue: -1020) when using URLSession when disconnecting from the internet.

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
            }
        }

CodePudding user response:

error code -1020 means dataNotAllowed. It means the connection failed because data use isn’t currently allowed on the device.

What you are trying to catch notConnectedToInternet error code -1009 means connection failed because the device isn’t connected to the internet.

So notConnectedToInternet is not same as dataNotAllowed. In this case check your device settings.

  • Related