This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Easiest way to determine whether iPhone internet connection is available?
Hi guys,
I'm developing an iPhone app. It works totally with internet.
When I run my app, how can I check if internet is active and it works correctly?
And if it's not I would to show a popup.
Thanks.
Be aware that Reachability is (in my opinion) a misnomer.
Reachability, including reachabilityWithHostName (as in Apple's example) only indicates whether the device has an available internet connection with which to reach the outside world.
IMPORTANTLY: it does not indicate whether the specified host is actually up and running.
To find out whether the remote host is actually up and running, you'll need to initiate a connection to the remote host and handle a timeout in the case that it is not.
There's no such thing as "Internet". There exists large number of computers and other hardware, linked together. Now, if you need a particular host, you can ping it (also in code, don't know if iPhone lets you do this, though). If you need to ensure that particular service on the particular host is up and running, your only option is to send a request and check the response.
Another vote for Apple's reachability example code here.
Note it's important to get this right - apparently it's grounds for app rejection if you provide a misleading error message relating to networking. For example if you say you could not reach myhost.com when in fact it's because there is no network connection.
Related
When creating a progressive web app how can i know that a mobile device is connected to a working internet connection cause it can be connected but not working.
The saying, "You never know until you try" is pertinent here. At the moment you want to check whether there is connectivity, simply fetch a remotely hosted resource with a cache-busting technique. It's generally best practice for that resource to be tiny in file size if you need to check regularly and often (such as 1×1 pixel gif). If you receive a network response in the 200 range, you have connectivity. Otherwise, for the moment at least, you probably don't.
Suppose I have a application which fetch data from network and then showing those data in application. But sometimes internet becomes disconnected so I am showing message that 'Internet not available'. But I want to keep user busy with the application. So I want to do following things but there are some problem in implementation.
1) When network disconnected I want to show message 'network disconnect' and I want to store all data in db.
Problem: How will I know network is not available without happening any event?
2) And when network is connected I want to fetch data and store in buffer.
Problem : Same, how will I know network is available rightnow?
This is quite common question and is best solved using Reachability class.
You can find a nice manual here: Testing Network Reachability
And a quick answer is the accepted answer here:
iPhone reachability checking
But be aware that it's best to check for host availabiility. I tested this code when device was connected to AdHoc WiFi (with no internet connectivity) and the result was: internet available via WiFi. So if you want to be sure - use host status.
EDIT: and this is probably the best example on SO (answer from iWasRobbed):
How to check for an active Internet connection on iOS or OSX?
I was wondering how I could ping a web server of mine when my application first starts. I am assuming I would do s via the appdelegate in the application? Is there any specific example I could use?
What is your problem actually? You want to know whether Internet and/or your server is available or you want to know when to ping it? If you want to know the network or host status then you can use Reachability sample app by Apple. This Rechability class is widely used.
Anyone have an idea on how I could check for a wifi connection where I don't necessarily need to connect to the internet? I've implemented wifi multiplayer in a game I'm working on, so I want to make sure and let the user know if there's no connection when they are trying to use that mode. The Reachability sample code seems to only work for situations where you have a host you are trying to route to. Since I have no knowledge of how other people's wifi will be set up, I need to be able to check for wifi without trying to route to a specific place.
Edit: Nevermind, I've got it. Just took a little more digging in the Reachability code.
You can check that you reach the access point (which will almost always be the iPhone's gateway, Internet or not). 802.11b/g/n with a route to the Internet and without a route to the Internet will appear exactly the same to the phone -- or any device, for that matter.
I could foresee asking the phone for its gateway and attempting to talk to it (ping, maybe?) but I'm unfamiliar with the Reachability approach. If indeed you have figured it out, I'd love to see an answer.
There is a method in the reachability class that returns an instance for wifi. I think it was called reachabilityForLocalWiFi.
I am trying to cover my bases with reachability so that my app doesn't get rejected by the App Store. I am familiar with the Reachability class that Apple provides in sample code. My question is, how best to implement this. I've seen that checking if the WWAN is accessible is not always best because it may be turned off for power consumption. So do I have to somehow enable it before checking for a connection. Just looking for steps to take. My app doesn't need a constant connection. It does some polling on a given interval that will require a connection and makes other various requests. Just trying to figure this out so I don't get rejected. Any help would be great.
Edit: Will reporting errors NSURLDomainError errors suffice for reporting reachability? It currently displays No Internet Connection and Can't Find Host. Seems that this is the type of information that the reachability example is used for.
Assuming that your application needs a connection to the internet, you could check the connectivity to a website that is always up (like e.g. google.com). The reachability sample code already contains a method to do just that.
I have not worked with the Reachability API enough to know how it reacts to the system switching off the WWAN for power saving. The only thing that comes to my mind is that you could try to establish a connection to the network so the system switches the WWAN on. Then you can run your reachability code to check what kind of connection you are working with. As said, I have not tried it, just an idea.