iOS devices scenario of wifi but no internet access - ios

I have implemented reachability into my app, however when the user is connected to a public wifi network but there is no internet connection due to the user having to login to the wifi, reachability still returns there is an internet connection.
Is there a way to test for this scenario?

In my opinion, one approach is that create a simple ping to your server or reliable hosts such as google. Your ping also set a short timeout (e.g. 5 seconds). If the ping is not success or timeout, it means your device is not real Internet connection. One more thing is the ping should just be called whenever your reachability says connected.

Related

How to find out if a Wi-Fi is with or without an internet connection

I'd like to find out if a Wi-Fi has internet connection or not. Sometimes our devices are connected with Wi-Fi, but the Wi-Fi doesn't have an internet connection.
NWPathMonitor or Connectivity thirdparty. I tried it, but it is not fool proof.
I know I can write a method to ping a server, but this is a very ineffective and costly affair.
Any ideas?
The only way to know for sure if a network has an Internet connection is to try and make a connection to the Internet. There is nothing on the device that you can query to get the answer.
Even if there were it would have to resort to polling/pinging since connectivity can change at any instant.
A WiFi connection can transition from no-Internet to Internet when the user completes a Hotspot login form. A WiFi connection can transition from Internet to no-Internet if the upstream Internet connection fails in some way. A WiFi connection may be connected to the Internet but not be able to communicate with the specific host your app needs due to a routing failure or some other issue.
For all of these reasons Apple actively discourages "pre flight" checks. Even if a check passes a network operation can still fail due to a change in network status that occurred a microsecond later. It is also unnecessary overhead as it is generally a safe assumption that most devices have some sort of Internet connection most of the time, particularly if the device is an iPhone.
You need to handle errors anyway. Just try the operation and see what happens. If there is no network connection it will fail pretty quickly.
If you want to provide more feedback to the user then you could begin actively checking Internet connectivity with exponential back off in response to a connection error.
That way your app isn't constantly "pinging".
You can also use the error to start a process that uses SCNetworkReachability to check connectivity for you. Rather than using that framework directly, particularly if you are writing in Swift, you might like to use something that wraps it and makes it more accessible like Ashley Mills' Reachability.swift

Why Apple is not bringing support to check whether a connection has active internet connection?

When I checked the apple Reachability class, it does return true even though the connected network doesn't have an active internet connection. So I'm having few doubts here -
Why is Reachability class made to throw YES if the packet can leave the local device even though it doesn't ensure it is delivered at the endpoint? Are there any special reasons to limit the ability?
Reachability has reachabilityWithHostName method still, YES will be thrown by class even if we add familiar hostname in the same network. If it doesn't ensure endpoint packet delivery why they provided this method?
Is there any other way to check whether there is an active internet connection in the connected network other than Reachability? Do we need to add our custom APIs or logic to tackle this thing?
I did connect an android phone to the same network, it instantaneously notified me the connection has no internet connection.

Is there a way to programmatically change network routes on an iOS device?

Scenario: I want to have an iOS device connect to a LAN that has no upstream Internet connection, yet still retain and use its cellular data connection.
Theoretically, it strikes me that the iOS wifi NIC and the cellular data NIC are similar enough to dual NICs in a PC that I should be able to set up routing such that any request to, say, 192.168.. goes through the wifi connection, and any other request goes through the cellular data connection.
I did a test to see if both NICs are active when the iOS device is connected to wifi by the following steps.
Ensure that my iPhone's wifi is off and that I have a good cellular data connection.
Disconnect my wifi router's Ethernet cable to my cable modem.
Connect my iPhone to the wifi router.
Use another iPhone that's connected only via cellular data to create a game of Words with Friends.
As soon as the other iPhone completed the first move, my iPhone received a notification that there was a new game to play.
This confirmed that the cellular data connection was indeed alive and well enough to receive push notifications despite the wifi NIC's being connected.
The question becomes, then, can an app programmatically connect to a given wifi network, set the cellular data network to be the default route, and route any requests to, say, 192.168.. to go through the wifi network?
I know this post is old, but I happen to have done some work on using multiple network interfaces on iOS.
My experiments showed that accessing via hostname results in iOS choosing the network interface it wants to use, and not trying any further interfaces if the host cannot be resolved.
If you know the DNS Server IP address for any Ethernet/WiFi based network, you can send a DNS request yourself, convert the hostname into an IP address and access via IP address. iOS will then use the correct interface.
My guess is, that if you have the private class IP address space accessible over both connections, there's probably nothing you can do to specify which network interface should be used.

How do I force an iOS app to use 3G instead of WiFi?

I want to use a messaging protocol that works fine over 3G, but not over some corporate firewalls. How can my app force the use of the cellular network when it fails to connect over WiFi even in the case that the WiFi network is reachable?
EDIT: After reading through the implementation of the Reachability class I remain unsure whether the two are indeed mutually exclusive. It could well be possible to discover both interfaces via gethostbyname(), which I might try just to see what comes out.
Use getifaddrs to enumerate the network interfaces. Look for ifa_name that starts with "pdp." This will be the cellular interface (e.g., pdp_ip0). Test (ifa_flags & IFF_UP) to make sure the interface is up, and get the IP address from ifa_addr. Then use bind() to bind your socket to that address.
Not sure how you activate the cellular interface if it is not already up. Normally, I just make a high level http call to get iOS to wake up the network, but not sure under what conditions the cellular service becomes active when wifi is also available. I suspect it is usually there as a fallback to the wifi.
http://iphonedevsdk.com/discussion/comment/120957
From the looks of that thread, it seems like its impossible. Your best bet may be to try using the protocol, and saying something like "please disconnect from wifi and retry" if it doesn't work.
This thread says just about the same thing:
how to programatically start 3g connection on iphone?
You can use the Reachability class which apple wrote in order to test whether the network connection is currently over 3G or WiFi, and even to check if a specific resource is available on the network.
You can use any of the socket library to force to use cellular. Find the cellular interface from getifaddrs, pass the interface to the socket function, socket bind will happen with cellular. e.g. CocoaAsyncSocket

iOS WiFi probe interval

I am writing an app which connects via WiFi to a proprietary device. The proprietary device acts as a WiFi access point.
When the device powers down, the WiFi connection is terminated, as I would expect.
The iPhone continues to send probe requests, looking for networks to connect to. It follows an exponential backoff algorithm for sending probes.
My problem is that eventually the intervals between the probe requests sent by the iPhone are longer than the timeout on my device, so they don't make a connection.
I am using the Reachability code and it works as I would expect.
It seems that pressing the home button will reset the backoff and send a probe request immediately. Does anyone know of a way to have my app do something similar?
Thanks for any help.
Instead of pinging the internet every time with Reachability, ping a host on the local network like the DNS server or the router (192.168.1.1).
I somehow experienced a similar situation to check if the device was connected in a specific VPN connection. The approach was to ping to a machine in the local network, via standard ping or implementing a ping web service.
If you don't have a backend in your local network then the easiest would be the ping. You can check the following code example from the Apple developer site:
http://developer.apple.com/library/mac/#samplecode/SimplePing/Introduction/Intro.html
I would suspect that if you reconfigure the connection (re-set the WiFi configuration) in your app that the iPhone would restart scanning without the backoff. So your app could keep track of how long it was since the connection was lost and then reconfigure the link after an appropriate amount of time. Possibly you would have to reconfigure to a different SSID and then switch back, depending on how smart the iOS-libraries are.
I'm not shure, if i understand you right. You want to check if the device is still connected to the access point?
I've build an app to connect to a scanner via wifi, the scanner acts as access point. for that i check if the SSID the device is currently connected to is the one of the scanner. to check the CurrentSSID you can use the following code:
+(NSString*)currentSSID {
CFArrayRef myArray = CNCopySupportedInterfaces();
CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
if (!myDict) {
return nil;
}
NSDictionary *myDictionary = (__bridge_transfer NSDictionary*)myDict;
return [myDictionary objectForKey:#"SSID"];
}
hope that helps.
I am afraid there is no way of doing so without being rejected during the review (you could read on how to access the SBWifiManager). Apple does not enable any way to access the wifi manager from the sandbox environment. I experienced similar issue connecting my device to various access points (for locating with probe request), up to now with the iOS 7 the probe requests were sent between huge intervals (even 15 min). Try to modify your access points.

Resources