- Home /
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.
Checked Application.internetReachability value on the iPhone and it shows "ReachableViaLocalAreaNetwork", but devise connected only to Wi-Fi. Does anybody had the same problem?
I think that means it's connected to wifi but not the internet. You sure your router has Internet access?
Yes I have Internet access. I tested in app purchases and it works, I tested ads then and it works, too. Well, I read some articles about WWW class and as I understand this class works good on iOS. And as I said all my functions for leader board work on Android device. Both Android and iOS devices connected to same wi-fi router.
Answer by OSG · Apr 10, 2014 at 06:04 AM
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
Answer by flamy · Apr 09, 2014 at 06:31 AM
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
I added your code and on iOS devices (tested on 2 iPhones) it shows that there is no connection. I have no idea why I can't reach the Internet via WWW class on iOS. But anyway, thank you for answer.
does it reach internet on browser??
should be the problem there!
Opened browser and successfully opened google.com page, after that opened game and isconnectedToInternet variable is False :( It is very strange that it works as it should on $$anonymous$$acBook, on PC with win 8.1 and Android phone but don't work on iOS. In Unity last updates there are some fixes with iOS and WWW class, so I updated Unity to the latest 4.3.4 version but it didn't help me.
im actually not sure if this would solve the problem, can you just try to switch on "Requires Persistent Wifi" in ios build settings -> other settings tab
I checked this setting before :) It os turned on in plist.info and when game is installed on iPhone there is internet permission in settings and it is turned on too. It looks like I missing just one little peace of this puzzle, but I can't find it.
Answer by tonic · Jun 27, 2014 at 02:13 PM
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
Answer by grujicbr · Aug 19, 2014 at 06:39 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