Internet reachability on iOS

Hi all. I have some problems with internet connection on iOS device. I have Android build of my game which is working perfect. I posting high scores to my web-server via WWW class. Everything works. But when I testing my app on iPhone there is some problem with posting and getting high scores. To check internet reachability I use Application.internetReachability and I have simple function which tries to get response from the server and if WWW class returns right answer from the server I set my special bool value to TRUE. So with turned on Wi-Fi on the iPhone app can’t get any response from server, and it seems that server doesn’t get any query from the app.

public class CheckInternet : MonoBehaviour {

    public static CheckInternet instance;
	const float CheckTimer = 5f;

	public bool isconnectedToInternet = false;

    void Awake()
    {
       
          instance = this;
    }

	void Start () {

		InvokeRepeating("PingService",0,CheckTimer);
	}

	void PingService()
	{
		StartCoroutine(PingGOOGLE());
	}

	IEnumerator PingGOOGLE()
	{
		WWW www = new WWW("www.google.com");

		yield return www;

		if(www.error == "" || www.error == null)
		{
			isconnectedToInternet = true;
		}
		else
		{
			isconnectedToInternet = false;
		}
	}

}

this continously pings google every “checkTimer” seconds and isConnectedToInternet gives you the status.

Attach the file to a gameobejct and when you want to check connection, use CheckInternet.instance.isconnectedToInternet == true

alternatively you can use Ping class which is useful for the same purpose especially if u have your own server and check for its rechability

Finally I found source of the problem. Firstly I added String.IsNullOrEmpty(www.error), not sure that it is necessary. And the main problem was with info.plist file. There is a string “Application uses Wi-Fi” and it’s value is YES. I thought that it’s enough for wi-fi availability on iOS device and I was wrong.

So to use Wi-Fi for WWW class we need to add “wifi” item to “Required device capabilities”. I don’t know why it was not added automatically. Hope this will help somebody. And big thanks for helping to @flamy

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

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