Ads wont show when the network is unavaliable. So I want a script or other method which can make the app stop unless network access is available.
-
2I have no idea why anyone would particularly downvote this question. It's (1) short (2) very clear (3) an interesting point in Unity, the answer is obscure unless you know how to do it. There was one minor grammar error which I fixed. – Fattie Feb 04 '16 at 15:15
1 Answers
Fortunately in Unity it is this simple,
Android
iOS
Open the Build Settings dialog (file menu)
Click PlayerSettings, look at Inspector, to to OtherSettings panel,
scroll down to InternetAccess ...
... and choose the mode you want!
That's it.
Advanced issues: Note too, this very useful call ...
http://docs.unity3d.com/ScriptReference/Application-internetReachability.html
which however is quite involved to use; it's a major project to implement this. In more complex projects, consider also
http://docs.unity3d.com/ScriptReference/NetworkReachability.html
If you want to get in to these, start off with the many starter examples posted online, for example https://stackoverflow.com/a/24351716/294884 (that person sensibly uses google's famous DNS as a "site you can almost always reach in theory if the internet is in existence" - just one of many problems to deal with when you try to implement this).
Finally, note that in a sense one way to do this is
just go to the internet for some page you know is there
if, after waiting a sufficient amount of time (how long would that be? maybe 5 seconds - I don't know) you don't get a result, then assume there's no decent internet connectivity.
Here's the actual code to get you going on that if you're never done a WWW
before in Unity.
private string testUrl = "http://someTestSite.com/whatever.html";
private void CheckIfWeCanGetNetPagesAtAll()
{
StartCoroutine(_Check());
}
private IEnumerator _Check()
{
WWW w = new WWW( testUrl.URLAntiCacheRandomizer() );
yield return w;
if (w.error != null)
{
Debug.Log(">>> could not get internet test page, Error .. " +w.error);
// bring up error message for user, that the app canot be used
// just no because you have no internet access.
}
else
{
// no problems
}
// not shown here ... you may prefer to implement your own time-out
// rather than just waiting for WWW to time-out, which can be long.
}
-
However, you can see this setting only in Android. So I think it's not a general answer. Am I right? – Hamza Hasan Feb 04 '16 at 15:20
-
1
-
It should be okay for most cases, but what if iPhone or iPad has connection with internet but not by WIFI? In that case this option is useless. – Jerry Switalski Feb 04 '16 at 15:56
-
@JoeBlow yes, I was refering to Requires Persistant Wifi for ios – Jerry Switalski Feb 04 '16 at 17:58
-
Hi Jezz, you're **right**, there is no absolutely final solution for this tricky problem, and the ios/android options dont really match. (note that both the android/ios options are in fact **buggy**, and indeed poorly defined.) This is a HUGE topic that is a huge pain in the ass, like i mentioned google for many discussions http://answers.unity3d.com/questions/682741/internet-reachability-on-ios.html – Fattie Feb 04 '16 at 18:05