Re: Checking for Internet Connection in an iApp
Re: Checking for Internet Connection in an iApp
- Subject: Re: Checking for Internet Connection in an iApp
- From: Kyle Sluder <email@hidden>
- Date: Sun, 16 Oct 2011 15:03:12 -0700
On Sun, Oct 16, 2011 at 2:45 PM, John Love <email@hidden> wrote:
> I really have been just about everywhere, e.g., stack overflow.com and iphonedevsdk.com and have really learned a lot … unfortunately, what I have learned is that there are opposing opinions on how to best check for an active internet connection with respect to effectiveness and speed. Such comments such as "You're not testing for what you think you are!".
The only way to test if you can connect to a host is to actually try
to connect to the host. Reachability can only tell you if the device
is aware that it has no route to that host. It's a good way to fail
quickly (for example, if the device knows all of its interfaces are
down), but the only authoritative answer can be gained by actually
attempting to connect.
>
> Considering my lack of success with Apple's Reachability Code, I have found that the asynchronous call to:
>
> NSString *urlString = @"myURL";
> NSURL *theURL = [NSURL URLWithString:urlString];
> NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL];
> NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
>
> which does hook up with the delegate method:
> - (void )connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
>
> This magic does indeed work, but I could fry a couple of eggs while I'm waiting and I know that the iApp folks will bounce me for that delay.
If you want a shorter timeout, call -setTimeoutInterval: on your
NSMutableURLRequest.
>
> Maybe my method of "taking my connection" down may be in error -- I am simply pushing off the "standby" button on my cable modem -- I have also just unplugged the modem.
No, that's a good test. It simulates an upstream problem.
You could also run in the Simulator and turn on the new Network Link
Conditioner in Lion (/Applications/Utilities/Network Link Conditioner)
if you want to simulate a poor 3G or EDGE connection.
> Isn't the function of -startNotifier to enable the callback method to be continuously called within the main RunLoop?
As the name of the notification indicates, the system only sends that
notification when the reachability has changed. It doesn't
periodically send out notifications. If you have previously refused to
attempt a connection because reachability said you had no route to the
host, you should try again when you receive the "reachability changed"
notification. You might early out again (host still not reachable), or
you might attempt to connect and either succeed or fail.
>
> Yet the only time I can get this callback method to be called is if I call NSNotificationCenter's -postNotificationName elsewhere in the code??
>
> -[notificationCenter postNotificationName:kReachabilityChangedNotification object:internetReachability_];
The system posts this notification. You should never post it yourself.
Hope that helps,
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden