How to check if internet is active and it works? [duplicate]
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.
精彩评论