1

I want to check the internet connection in my iPhone app.

The app needs to know, if the phone is connected via wifi and if wifi is connected with the strict router.

For example: It is a app to order drinks in a bar. The user is only allowed to order something if he is connected to the network of the bar.

I tried:

- (IBAction)buttonCHECKNETWORK:(id)sender{

struct sockaddr_in callAddress;
callAddress.sin_len = sizeof(callAddress);
callAddress.sin_family = AF_INET;
callAddress.sin_port = htons(24);
callAddress.sin_addr.s_addr = inet_addr("152.465.456.975");

 Reachability *hostReach = [Reachability reachabilityWithAddress:&callAddress];

NetworkStatus netStatus = [hostReach currentReachabilityStatus];

if (netStatus == NotReachable)
{
    NSLog(@"IP NotReachable");
}

if (netStatus == ReachableViaWiFi)
{
    NSLog(@"IP ReachableViaWiFi");
}

if (netStatus == ReachableViaWWAN)
{
    NSLog(@"IP ReachableViaWWAN");
} }

But every IP is reachable (also this fictional IP in my code)... I would like to put the Router IP in 'callAddress'. Or the name of the router or something like that.. It would not be reachable if the user is connected via 3G.

Paul R
  • 208,748
  • 37
  • 389
  • 560
kerimrohs
  • 69
  • 1
  • 9

1 Answers1

1

By getting the local IP address (see iPhone Obtaining IP Address When Connected Over 3G for example - and note this solution provides for the Wi-Fi address as well) you could compare this to the fixed IP address range of your bar's router and see if you are in the same network.

Community
  • 1
  • 1
Peter M
  • 7,309
  • 3
  • 50
  • 91
  • Works! Thank you! Is it possible to get the network name too? The comparison would become a little bit safer with the IP address AND the network name. – kerimrohs Jan 18 '12 at 15:04
  • What do you mean by "Network Name"? Do you mean the WiFi's SSID or do you mean the DNS name? And what do yo mean by "Safer"? If you are worried about someone spoofing your WiFi network then you need to look into something more robust than just using the IP address for authentication. – Peter M Jan 18 '12 at 15:28
  • 1) I mean SSID! It doesn't need to be 100% safe, nobody of the user will change his home network to get access from home.. But SSID would be a little bit 'safer', because the ip (i took the first 6 numbers) could be the same like the ip at home...by randomly.. 2) But do you know any other (easy) way, which is safe? – kerimrohs Jan 18 '12 at 17:19
  • This apparently works on iOS 4.1 and greater for getting the SSID: http://stackoverflow.com/questions/5198716/iphone-get-ssid-without-private-library – Peter M Jan 18 '12 at 18:28
  • Thanks for the link! But i can't get the SSID name out of the context. The Output (Log) is right, but i want to compare it (SSID) and not just write it down. Is it possible? The method is too difficult for me ...sorry, maybe you can help me – kerimrohs Jan 19 '12 at 15:21
  • can u help me a last time? is it possible? – kerimrohs Jan 22 '12 at 16:41