It seems there is a bit of an issue with the above code and it may need to be modified depending upon your intended use. I put this code into my app (original code provided by Apple; thank you Apple) and it only worked when the iPhone was on a WiFi connection. I did some research and testing to come up with a solution.
There are a set of flags that can be used to determine connectivity (
iPhone Dev Center). Not being entirely sure which to use, I set up a tester that would show me which flags are active when I am using different connection methods. This is something that I would recommend anyone to try so that you can get a better understanding of how it works, however this is the change I made that seemed to fix the issue:
Code:
...
isReachable = flags & kSCNetworkFlagsReachable;
needsConnection = flags & kSCNetworkFlagsConnectionRequired;
nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
return ((isReachable && !needsConnection) || nonWiFi) ? YES : NO;
...
If someone knows a better way or if im completely wrong here, please let me know.
The problem I am trying to research now is that once the iPhone has left the WiFi connection and gone on to the 3G or Edge networks, it wont go back onto the WiFi again even if I am in range of the signal. I have to exit my app and either wait or reinitialize it in the settings. Does anyone know a way around this?