Gabi Dobre
Gabi Dobre

Reputation: 63

How can I make a Unity app work, only if network is avaliable

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.

Upvotes: 1

Views: 486

Answers (1)

Fattie
Fattie

Reputation: 12582

Fortunately in Unity it is this simple,

Android

enter image description here

iOS

enter image description here

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

Upvotes: 4

Related Questions