- Home /
How can one test if there is any internet (wi-fi or ethernet) connection available ?
My current game deals with all cases when an internet connection is available but does not deal with the case when there is no internet connection, resulting in nasty errors of course which should be dealt with before they occur by testing if the Network operations can be performed in the first place.
When trying to run the test the connection with the example from the reference guide, if no internet connection is present, this error occurs:
"Cannot resolve connection tester address, you must be connected to the internet before performing this or set the address to something accessible to you."
To completely cover all the cases, I need to allways know if there is an internet connection. Polling the network connection with the function Network.TestConnection() does not work at all and does not address my issue since it refers mainly to NAT punchthrough statuses.
Thanks in advance for any help out there :)
Answer by paulsz · Dec 13, 2010 at 06:12 PM
I have posted the same issue on the forums and iceshaft07 kindly shared his solution with me:
Thx alot guys
Cheers to iceshaft07 :D
I prefer this solution: http://answers.unity3d.com/answers/343863/view.html
Answer by Tuor · Oct 19, 2011 at 07:20 PM
Look for Application.internetReachability in the scripting api.
Answer by Loius · Nov 29, 2010 at 03:25 PM
Does a try-catch block help?
try {
Connect();
} catch ( e ) {
Debug.Log( "Connect error: " + e.message);
}
Sadly this does not help :( I already tried this approach but the fact of the matter is that the error is triggered within unity engine long after (a few seconds even) the Network.Connect() method is called since connection is not done immediately as the function is called.
I'm sure the solution should be like a polling system (the same as used when looking for hosts ) which checks every frame if there is internet connection. It surprises me that I have not found a single thread about this problem on the forums or on the answers since this is a pretty big deal. Thank you for your response :)
You might try "yield Network.Connect();", if .Connect is a 'real' coroutine that should wait for it to finish and maybe the error will be reportable? Unfortunately I haven't worked at all with internetness; hopefully someone else will come in here and know something at you!
Thx for the answer Vicenti,
It would have been awesome to be able to do it like you said but you cannot put a yield statement in a try catch block-> try { yield return Network.Connect( "address" ); } catch ( System.Exception aException ) { }
gives the compilation error: "error CS1626: Cannot yield a value in the body of a try block with a catch clause".
Anyway, I think I might have figured it out in a not very elegant manner: Just check every frame if there is Wifi with iPhoneSettings.internetReachability. Unfortunately this only works on the iphone
Answer by tonic · Jul 06, 2014 at 01:05 PM
(Adding a comment to an old posting in case somebody finds this using search...)
To truly know you're online, you need to implement "captive portal detection", to know if you're e.g. hitting a public WiFi login page. So just checking Application.internetReachability or doing a Ping to some address doesn't guarantee you can successfully make connections or make WWW requests.
I have made an easy asset called Internet Reachability Verifier. It keeps you up-to-date whether you have verified internet access (WWW requests can be done). Works with Desktop, Mobile and Webplayer platforms (need to self-host a file in originating server for Webplayer).
More info here: http://j.mp/IRVUN
Answer by grujicbr · Aug 19, 2014 at 09:55 PM
Hi,
I have released a event based plugin for ios that lets you do exactly this without having to do it manually. It is very easy to use and works on all ios devices. Android version is on works as well
Your answer
Follow this Question
Related Questions
How to check iphone network connection 0 Answers
MSSQL Connection and Select 2 Answers
Occasionally issue with Wifi connection 0 Answers
Network check does not work on iOS 1 Answer
Check if an array index is empty 1 Answer