Search Unity

How to check internet connection in an app

Discussion in 'Scripting' started by sunartoryan, Feb 7, 2016.

  1. sunartoryan

    sunartoryan

    Joined:
    Feb 7, 2016
    Posts:
    5
    Hello all.
    I just a newbie in the programming Unity. And I here have a trouble.

    I have an app, and I want this app when running in Android/iOS platform, this app is connected to internet. If the internet connection is off, this app show a dialogue box : "Please connect to internet" & disabling all features or cannot run, until it connected to internet.

    Please help me to create the C# script in unity. And where I place this script? in gameObject or else..?

    Thank you very much.
     
    guetta18 and TheKingOfTheRoad like this.
  2. domkia

    domkia

    Joined:
    Aug 19, 2012
    Posts:
    99
  3. Lishin

    Lishin

    Joined:
    Jun 11, 2016
    Posts:
    1
    Code (CSharp):
    1.  if(Application.internetReachability == NetworkReachability.NotReachable)
    2.         {
    3.             Debug.Log("Error. Check internet connection!");
    4.         }
     
  4. wildDevelop

    wildDevelop

    Joined:
    Oct 9, 2016
    Posts:
    1
    Best answer , clean and easy.
     
  5. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    229
    And wrong. You don't use this to check if you actually can reach the internet, but to distinguish between WiFi and Carrier networks.
     
    no-luck, Novack, ArtTeam and 3 others like this.
  6. HonorableDaniel

    HonorableDaniel

    Joined:
    Feb 28, 2007
    Posts:
    2,829
    I would just make a simple WWW call to some webpage. If it returns success, you have internet, if it returns an error, you don't.
     
    TheKingOfTheRoad likes this.
  7. Mazyod

    Mazyod

    Joined:
    Apr 21, 2014
    Posts:
    24
    If you need 100% reachability check, it's not a matter of the user being connected to "a" network. It's a matter of the user being able to reach a certain service that you should carefully pick.

    Personally, I opt-in for a simple endpoint on my servers that the users try to reach, and if that succeeds, I can guarantee they can use my game services.

    One of the many cases they might be connected to a network but can't reach your services are captive networks and intranets.
     
  8. elenzil

    elenzil

    Joined:
    Jan 23, 2014
    Posts:
    65
    This does not seem quite right. Application.internetReachability returns a NetworkReachability enum, which has three values: WiFi/Wired, Cellular, and None. So you can use this to detect say "you have no carrier and no wifi".

    The other comments here that you probably want to also ping some low-cost endpoint on whatever server/s you're interested in still hold true, for the reasons other folks have mentioned.
     
    TheKingOfTheRoad and WILEz1975 like this.
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,529
    I'd verify you can actually connect to your specific game service rather than a generic internet connection check. Users may be attached to a public wifi that requires a webpage sign in, where some services are allowed without sign in but others are blocked. Firewalls on public or private corporate wifi may block certain ports that may be important to you, but let through traffic you would use for a generic internet check. Really only checking the connection to your actual game service is the closest thing to a guarantee.
     
    TheKingOfTheRoad likes this.
  10. ovirta

    ovirta

    Joined:
    Mar 20, 2015
    Posts:
    36
    I agree. You can definitely test whether network is present with Application.internetReachability. Additionally pinging endpoint will verify data transfer capability.
     
    Last edited: Sep 7, 2017
    TheKingOfTheRoad likes this.
  11. Hoorza

    Hoorza

    Joined:
    May 8, 2016
    Posts:
    20
    TheKingOfTheRoad and ovirta like this.
  12. tbigham

    tbigham

    Joined:
    Mar 6, 2017
    Posts:
    1
    How would you interpret the case where Application.internetReachability returns NetworkReachability.NotReachable?
     
    TheKingOfTheRoad and ovirta like this.
  13. TheKingOfTheRoad

    TheKingOfTheRoad

    Joined:
    Apr 24, 2013
    Posts:
    215
    Years later but...Sounds cool. In my case im using Photon, so i will go to check the game has... data transfer capability.

    if (!PhotonNetwork.connected)
    {
    // do something
    }
     
    Hoorza likes this.
  14. AlexCarrington

    AlexCarrington

    Joined:
    Apr 9, 2015
    Posts:
    21
    If your game/app uses Google Play Games you can simply pause at the splash screen and wait for a google signin to continue playing.
     
  15. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    818
    Just ping google.com, since it's pretty much guaranteed to be up :)
     
  16. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,529
    You'll get false negative results on corporate networks which sometimes are configured to block ICMP packets from passing through the firewall. The network I wrote this message on for example.
     
  17. jfa257

    jfa257

    Joined:
    Jul 17, 2013
    Posts:
    4
    I think some people are misreading the documentation. They tell you not to use to check internet availability IF the devices are handheld. If not, this works perfect since the wifi/lan internet option is true or false when NetworkReachability.ReachableViaLocalAreaNetwork. is checked. I think it would work still on handhelds, since the hotspot has to create some sort of network using the tethered device as a router. Maybe the refer to the innacuracy of the class to properly detect if there is internet feed on a handheld connected to a hotspot network. In any case, if the connection is strong enough, it will get detected, if not, although present for email or other apps, don't think would be of much use for multiplayer connectivity though, so it's a moot point if the NetworkReachability class can accurate see if a handheld is receiving internet or not.